How can I transform values from an array to a new array?
I have an array like:
[
{ "amount" : 10, "date" : "2017-05-30" }...
]
which I want to "transform" into
[
{ x : 10, y: 2017-05-30 }
]
any help is appreciated...
How can I transform values from an array to a new array?
I have an array like:
[
{ "amount" : 10, "date" : "2017-05-30" }...
]
which I want to "transform" into
[
{ x : 10, y: 2017-05-30 }
]
any help is appreciated...
You could map new objects with the new properties.
var array = [{ amount: 10, date: "2017-05-30" }],
newArray = array.map(a => ({ x: a.amount, y: a.date }));
console.log(newArray);
Using Array#map()
The callback function will create a new object with the 2 properties needed. You just have to keep the value of the current array
const array = [{ "amount" : 10, "date" : "2017-05-30" }];
let newArray = array.map(_=>({x:_.amount,y:_.date}));
console.log(newArray);
You can try using lodash map Lodash map
It will be something like:
const array2 = _.map(array1, (item) => { x: item.amount, y: item.date });