Page 1 of 1

Split an array of objects into separate arrays

Posted: 11 Nov 2020, 11:27
by Marius
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?

Split an array of objects into separate arrays

Posted: 11 Nov 2020, 13:17
by Admin
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);