The array in this order:
I want to create a new array as:
You could try something like this:
resultArray = yourarray.map(function(data){
return {
series:{
"data":[data.points.a,data.points.b]
}
}
})
You can apply map() function on your data to get your desired output like you posted above. According to question, a possible solution would be as below
const data = [{
"date": "2020-01-01",
"point": {
"a": "1",
"b": "2"
}
},
{
"date": "2020-02-01",
"point": {
"a": "3",
"b": "4"
}
},
{
"date": "2020-03-01",
"point": {
"a": "5",
"b": "6"
}
},
{
"date": "2020-04-01",
"point": {
"a": "7",
"b": "8"
}
}
];
const res = data.map((_, index) => {
return {
series: {
data: data.slice(0, index + 1).map(i => [Number(i.point.a), Number(i.point.b)])
}
}
});
console.log(res);
I believe this will get you what you want:
const orig = [
{
"date": "2020-01-01",
"point": {
"a": "1",
"b": "2"
}
},
{
"date": "2020-02-01",
"point": {
"a": "3",
"b": "4"
}
},
{
"date": "2020-03-01",
"point": {
"a": "5",
"b": "6"
}
},
{
"date": "2020-04-01",
"point": {
"a": "7",
"b": "8"
}
}
];
// Make the new array.
const points = [];
const newArray = orig.map( x => {
points.push([x.point.a, x.point.b]);
return {
"series": {
"data": points.slice(0)
}
}
});
// Log the new array.
console.log(newArray);