

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array of objects to array of arrays in JavaScript
Suppose, we have an array of objects like this −
const arr = [ {"Date":"2014","Amount1":90,"Amount2":800}, {"Date":"2015","Amount1":110,"Amount2":300}, {"Date":"2016","Amount1":3000,"Amount2":500} ];
We are required to write a JavaScript function that takes in one such array and maps this array to another array that contains arrays instead of objects.
Therefore, the final array should look like this −
const output = [ ['2014', 90, 800], ['2015', 110, 300], ['2016', 3000, 500] ];
Example
The code for this will be −
const arr = [ {"Date":"2014","Amount1":90,"Amount2":800}, {"Date":"2015","Amount1":110,"Amount2":300}, {"Date":"2016","Amount1":3000,"Amount2":500} ]; const arrify = (arr = []) => { const res = []; const { length: l } = arr; for(let i = 0; i < l; i++){ const obj = arr[i]; const subArr = Object.values(obj); res.push(subArr); }; return res; }; console.log(arrify(arr));
Output
And the output in the console will be −
[ [ '2014', 90, 800 ], [ '2015', 110, 300 ], [ '2016', 3000, 500 ] ]
- Related Questions & Answers
- Convert array of arrays to array of objects grouped together JavaScript
- JavaScript Converting array of objects into object of arrays
- Convert array of objects to an object of arrays in JavaScript
- Extract arrays separately from array of Objects in JavaScript
- Search from an array of objects via array of string to get array of objects in JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- JavaScript - length of array objects
- Compare array of objects - JavaScript
- Manipulating objects in array of objects in JavaScript
- Using methods of array on array of JavaScript objects?
- Creating an array of objects based on another array of objects JavaScript
- Filtering array of objects in JavaScript
- Combine array of objects in JavaScript
- Converting array of objects to an object of objects in JavaScript
- JavaScript: create an array of JSON objects from linking two arrays
Advertisements