
- 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
How to get single array from multiple arrays in JavaScript
Suppose, we have an array of arrays like this −
const arr = [ [ {"c": 1},{"d": 2} ], [ {"c": 2},{"d": 3} ] ];
We are required to write a JavaScript function that takes in one such array as the first and the only argument.
The function should then convert the array (creating a new array) into array of objects removing nested arrays.
Therefore, the final output should look like this −
const output = [{"c": 1},{"d": 2},{"c": 2},{"d": 3}];
Example
const arr = [ [ {"c": 1},{"d": 2} ], [ {"c": 2},{"d": 3} ] ]; const simplifyArray = (arr = []) => { const res = []; arr.forEach(element => { element.forEach(el => { res.push(el); }); }); return res; }; console.log(simplifyArray(arr));
Output
And the output in the console will be −
[ { c: 1 }, { d: 2 }, { c: 2 }, { d: 3 } ]
- Related Articles
- JavaScript: Combine highest key values of multiple arrays into a single array
- Get the smallest array from an array of arrays in JavaScript
- How to sum elements at the same index in array of arrays into a single array? JavaScript
- How to get multiple rows in a single MySQL query?
- Function to flatten array of multiple nested arrays without recursion in JavaScript
- How to Map multi-dimensional arrays to a single array in java?
- How can I get the output of multiple MySQL tables from a single query?
- Extract arrays separately from array of Objects in JavaScript
- Cartesian product of multiple arrays in JavaScript
- How to get all combinations of some arrays in JavaScript?
- How to get the difference between two arrays in JavaScript?
- How to filter out common array in array of arrays in JavaScript
- Make an array of multiple arrays in MongoDB?
- Finding intersection of multiple arrays - JavaScript
- How to join multiple arrays in TypeScript?

Advertisements