Split an array of objects into separate arrays
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
Marius
- Posts:107
Split an array of objects into separate arrays
I have an array of objects, and for plotting a graph I need to split it into 3 different arrays. How to do it using JS?
Code: Select all
const dataOverview = [{
id: 1,
series1: 31,
series2: 11,
timestamp: "2018-09-19T00:00:00.000Z"
},
{
id: 2,
series1: 40,
series2: 32,
timestamp: "2018-09-19T03:30:00.000Z"
},
{
id: 3,
series1: 28,
series2: 45,
timestamp: "2018-09-19T06:30:00.000Z"
}
];
I need to get such arrays
Code: Select all
var series1 = [];
var series2 = [];
var timeStamp = [];
What is the best way to accomplish this in JavaScript?
Admin
Posts:805
It is very simple, you just use the
map() function.
Code: Select all
var series1 = dataOverview.map(x => x.series1);
var series2 = dataOverview.map(x => x.series2);
var categories = dataOverview.map(x => x.timestamp);