
- 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
Merging nested arrays to form 1-d array in JavaScript
Problem
We are required to write a JavaScript function that takes in two nested arrays, arr1 and arr2, as the first and the second argument.
Our function should create and return a third array that contains all the elements of arr1 and arr2 but flattened to single dimension
For example, if the input to the function is −
const arr1 = [ 1, [ 2, [ 4, 5, [ 6 ] ] ] ]; const arr2 = [ 11, 12, [ 16, 18, [ 19, 21, [ 23 ] ] ] ];
Then the output should be −
const output = [1, 2, 4, 5, 6, 11, 12, 16, 18, 19, 21, 23];
Example
Following is the code −
const arr1 = [ 1, [ 2, [ 4, 5, [ 6 ] ] ] ]; const arr2 = [ 11, 12, [ 16, 18, [ 19, 21, [ 23 ] ] ] ]; const flattenAndMerge = (arr1 = [], arr2 = []) => { const res = []; const flatten = (arr = []) => { for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ flatten(arr[i]); }else if(typeof arr[i] === 'number'){ res.push(arr[i]) }; }; }; flatten(arr1); flatten(arr2); return res; }; console.log(flattenAndMerge(arr1, arr2));
Output
Following is the console output −
[ 1, 2, 4, 5, 6, 11, 12, 16, 18, 19, 21, 23 ]
- Related Articles
- Merging and rectifying arrays in JavaScript
- Alternatively merging two arrays - JavaScript
- Merging sorted arrays together JavaScript
- Merging two sorted arrays into one sorted array using JavaScript
- Merging Arrays in Perl
- Merging two arrays in a unique way in JavaScript
- Function to flatten array of multiple nested arrays without recursion in JavaScript
- Merging elements of two different arrays alternatively in third array in C++.
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Merging boolean array with AND operator - JavaScript
- Join arrays to form string in JavaScript
- Grouping nested array in JavaScript
- Convert nested array to string - JavaScript
- Simplifying nested array JavaScript
- Stack 1-D arrays as columns into a 2-D array in Numpy

Advertisements