如何将数组转换为新数组

时间:2017-05-30 13:00:22

标签: javascript

如何将数组中的值转换为新数组?

我有一个像:

这样的数组
{{ filter.name }}

我想“转化”成

[
  { "amount" : 10, "date" : "2017-05-30" }...
]

感谢任何帮助...

3 个答案:

答案 0 :(得分:7)

您可以使用新属性映射新对象。

var array = [{ amount: 10, date: "2017-05-30" }],
    newArray = array.map(a => ({ x: a.amount, y: a.date }));

console.log(newArray);

答案 1 :(得分:4)

使用Array#map()

回调函数将创建一个具有所需2个属性的新对象。您只需保留当前数组的值

const array = [{ "amount" : 10, "date" : "2017-05-30" }];

let newArray = array.map(_=>({x:_.amount,y:_.date}));
console.log(newArray);

答案 2 :(得分:0)

您可以尝试使用lodash map Lodash map

这将是:

const array2 = _.map(array1, (item) => { x: item.amount, y: item.date });
相关问题