我有一个像这样的对象:
var days = {
0: 10,
1: 40
2: 20,
3: 15,
4: 5,
5: 18,
6: 9
};
我需要将其转换为类似的东西:
var days2 = [
{0:10},
{1: 40},
{2: 20},
{3: 15},
{4: 5},
{5: 18},
{6: 9},
];
我知道这很容易,但我不知道
答案 0 :(得分:2)
您可以获取对象的所有条目,并使用键/值对构建新对象。
var days = { 0: 10, 1: 40, 2: 20, 3: 15, 4: 5, 5: 18, 6: 9 },
result = Array.from(Object.entries(days), ([k, v]) => ({ [k]: v }));
console.log(result);

答案 1 :(得分:0)
您可以使用Object.keys
和map
Object.keys(days).map( key => {return { [key]: days[key]
var days = {
0: 10,
1: 40,
2: 20,
3: 15,
4: 5,
5: 18,
6: 9
};
var output = Object.keys(days).map( key => {return { [key]: days[key] } });
console.log(output);