cfa*_*had 3 javascript arrays foreach object
我有一个具有不同值的对象。
let arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},
{"Fashion": "1300"},{"Documentary": "2600"}]
Run Code Online (Sandbox Code Playgroud)
我怎样才能 foreach 它们,然后将它们放在一个对象中并为它们选择一个名称,如下所示:
"photo_type": {
"Personal": "1000",
"sport": "2100",
"Industrial": "1200",
"Commercial": "2300",
"Fashion": "1300",
"Documentary": "2600"
}
Run Code Online (Sandbox Code Playgroud)
我不希望它们像 0、1 和 2。
您可以使用Object.assign
和 通过扩展数组来创建对象。
let array = [{ Personal: "1000" }, { sport: "2100" }, { Industrial: "1200" }, { Commercial: "2300" }, { Fashion: "1300" }, { Documentary: "2600" }],
object = Object.assign({}, ...array);
console.log(object);
Run Code Online (Sandbox Code Playgroud)