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: 106
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
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);
Similar Topics
-
Dot product of two arrays in Javascript
JavaScript - jQuery - Ajax
First post
What's an efficient way of implementing the dotProduct method (to get the Dot product of two arrays) without importing any new Javascript libraries?...
Last post
Here is a method.
Use the map() function to create a new array with multiplied results of each index and the reduce() function to sum the values of...
-
Looping through mulitple arrays
PHP - MySQL
First post
I am working to return reviews for a list of Agents. I have a foreach loop that then pulls the list of reviews for each agent. The list is returned...
Last post
Try to check if it is a multidimensional array, use count($agent, 1);, it will recursively count the array.
$ag1 =[
0 => ,
1 =>
];
$ag2...
-
Random object from multiple arrays with percent chance in JS
JavaScript - jQuery - Ajax
First post
I have 3 arrays of objects in JavaScript:
const fruits =
const car =
const books =
One temporary array where I will store random objects...
Last post
You need to get two random numbers:
- the first to decide which group to pick,
- the second to pick an item from that.
We generate a random...
-
Push an array into the same array JS
JavaScript - jQuery - Ajax
First post
I'm trying to push an array into the same array in javascript, But it doesn't seem to be working; the third element is added continuously.
Here is...
Last post
You are trying to push the same reference to the array. So, when the array updated later, the array inside the element will be updated as well.
To...
-
Check if multidimensional array
PHP - MySQL
First post
How can I check in php if an array is multidimensional or not?
I get a JSON from a third-party application and I parse it in php. But sometimes data...
Last post
You can check with two count() functions. If you add a second argument as True it will recursively count the array.
Here is an example:
$arr =[...