
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Extract arrays separately from array of Objects in JavaScript
Suppose, we have an array of objects like this −
const arr = [{ name : 'Client 1', total: 900, value: 12000 }, { name : 'Client 2', total: 10, value: 800 }, { name : 'Client 3', total: 5, value : 0 }];
We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property.
Therefore, one array for the name property of each object, one for total and one for value. If there existed more properties, we would have separated more arrays.
Example
The code for this will be −
const arr = [{ name : 'Client 1', total: 900, value: 12000 }, { name : 'Client 2', total: 10, value: 800 }, { name : 'Client 3', total: 5, value : 0 }]; const separateOut = arr => { if(!arr.length){ return []; }; const res = {}; const keys = Object.keys(arr[0]); keys.forEach(key => { arr.forEach(el => { if(res.hasOwnProperty(key)){ res[key].push(el[key]) }else{ res[key] = [el[key]]; }; }); }); return res; }; console.log(separateOut(arr));
Output
And the output in the console will be −
{ name: [ 'Client 1', 'Client 2', 'Client 3' ], total: [ 900, 10, 5 ], value: [ 12000, 800, 0 ] }
- Related Articles
- Extract unique objects by attribute from an array of objects in JavaScript
- Array of objects to array of arrays in JavaScript
- JavaScript: create an array of JSON objects from linking two arrays
- Extract a property from an array of objects in PHP
- JavaScript Converting array of objects into object of arrays
- Convert array of arrays to array of objects grouped together JavaScript
- Convert array of objects to an object of arrays in JavaScript
- How can we make an Array of Objects from n properties of n arrays in JavaScript?
- Sorting parts of array separately in JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- How do we loop through array of arrays containing objects in JavaScript?
- Search from an array of objects via array of string to get array of objects in JavaScript
- Extract unique values from an array - JavaScript
- Removing duplicate objects from array in JavaScript
- Get the smallest array from an array of arrays in JavaScript

Advertisements