小编Hus*_*ala的帖子

从数组创建对象

我想从数组列表中创建对象.我有一个动态的数组,假设看起来像这样:

var dynamicArray = ["2007", "2008", "2009", "2010"];

和一些javascript es6我想做一个像这样的对象:

const obj = {
    2007: {
        x: width / 5,
        y: height / 2
    },
    2008: {
        x: (2 / 5) * width,
        y: height / 2
    },
    2009: {
        x: (3 / 5) * width,
        y: height / 2
    },
    2010: {
        x: (4 / 5) * width,
        y: height / 2
    }
}
Run Code Online (Sandbox Code Playgroud)

不要担心内部对象,但只是想要这样的make结构:

 obj = {
      2007: ...,
      2008: ...,
      ...
    }
Run Code Online (Sandbox Code Playgroud)

请帮忙,谢谢.

javascript arrays javascript-objects ecmascript-6

36
推荐指数
5
解决办法
5万
查看次数