flatten nested array javascript
JavaScript
// using recursion, .reduce() and .concat() methods
// works with arrays of any depth
function flatten(arr)
{
return arr.reduce((acc, cur) => acc.concat(Array.isArray(cur) ? flatten(cur) : cur), []);
};
const arr = [[1,2],[3,[4,[5]]]];
const flattened = flatten(arr);
console.log(flattened);
/*
Output: [ 1, 2, 3, 4, 5 ]
*/// flat(depth),
// depth is optional: how deep a nested array structure
// should be flattened.
// default value of depth is 1
const arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
const arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]var multiDimensionArray = [["a"],["b","c"],["d"]]; //array of arrays
var flatArray = Array.prototype.concat.apply([], multiDimensionArray); //flatten array of arrays
console.log(flatArray); // [ "a","b","c","d"];
Also in JavaScript:
- Title
- closure javascript
- Category
- JavaScript
- Title
- ionic cordova icon notification
- Category
- JavaScript
- Title
- how to add elements in javascript html
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- javascript change url without reload
- Category
- JavaScript
- Title
- combine values of address line 1 and address line 2 javascript
- Category
- JavaScript
- Title
- how to get array values in javascript
- Category
- JavaScript
- Title
- array js fill
- Category
- JavaScript
- Title
- get all entries in object as array hjs
- Category
- JavaScript
- Title
- for in loop javascript
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- javascript class inheritance
- Category
- JavaScript
- Title
- javascript check if string ends with
- Category
- JavaScript
- Title
- how to delete element in list javascript
- Category
- JavaScript
- Title
- convert base64 to uint8array javascript
- Category
- JavaScript
- Title
- javascript change element id
- Category
- JavaScript
- Title
- datatable after. draw
- Category
- JavaScript
- Title
- check if function exists javascript
- Category
- JavaScript
- Title
- headroom js react
- Category
- JavaScript
- Title
- how do i backspace from javascript calculator
- Category
- JavaScript
- Title
- checked unchecked through js
- Category
- JavaScript
- Title
- if object is array javascript
- Category
- JavaScript
- Title
- devextreme custom field
- Category
- JavaScript
- Title
- for(let [key,val] in obj){ messageBody = messageBody.replace("{"+ key + "}",val) }
- Category
- JavaScript
- Title
- insert item into array specific index javascript
- Category
- JavaScript
- Title
- (function (g, d, a) {})(window, document, jQuery);
- Category
- JavaScript
- Title
- How to check whether a string contains a substring in JavaScript?
- Category
- JavaScript
- Title
- how to build a string javascript es6
- Category
- JavaScript
- Title
- change js to json
- Category
- JavaScript
- Title
- if back react
- Category
- JavaScript
- Title
- javascript check if element is visible on screen
- Category
- JavaScript
- Title
- dotenv not loading process.env in node
- Category
- JavaScript
- Title
- get args nodejs
- Category
- JavaScript
- Title
- adonis js sort by
- Category
- JavaScript
- Title
- dropzone add download button addedfile
- Category
- JavaScript
- Title
- devexpress dxdatagrid custom text field
- Category
- JavaScript
- Title
- how to make background image move mmousemove jquery
- Category
- JavaScript
- Title
- equal to or more than javascript
- Category
- JavaScript
- Title
- array of objects javascript
- Category
- JavaScript
- Title
- average of an array js
- Category
- JavaScript
- Title
- how to create request body javascript
- Category
- JavaScript
- Title
- how to return an object in javascript
- Category
- JavaScript
- Title
- express js continous GET /json/version
- Category
- JavaScript
- Title
- express validator always return error
- Category
- JavaScript
- Title
- ajax .put
- Category
- JavaScript
- Title
- conditional rendering with radio buttons
- Category
- JavaScript
- Title
- divide intagers javascript
- Category
- JavaScript
- Title
- how to check if a number is even or odd in javascript
- Category
- JavaScript
- Title
- how to create component in reactjs
- Category
- JavaScript
- Title
- javascript change page title
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- axios set authorization header
- Category
- JavaScript
- Title
- auto refresh page javascript
- Category
- JavaScript
- Title
- Javascript clear canvas
- Category
- JavaScript
- Title
- get only last 2 digit of year in javascript
- Category
- JavaScript
- Title
- axios post data vue js
- Category
- JavaScript
- Title
- javascript change url hash
- Category
- JavaScript
- Title
- Find channel discord js
- Category
- JavaScript
- Title
- express get params after ?
- Category
- JavaScript
- Title
- check if number appears odd number of times in array javascript
- Category
- JavaScript
- Title
- how to render different pages in nodes
- Category
- JavaScript
- Title
- how to add button react native app.js
- Category
- JavaScript
- Title
- how to print numbers in javascript
- Category
- JavaScript
- Title
- insert into specific array index
- Category
- JavaScript
- Title
- blob to text javascript
- Category
- JavaScript
- Title
- document ready js
- Category
- JavaScript
- Title
- array of images javascript
- Category
- JavaScript
- Title
- crear componente con angular cli
- Category
- JavaScript
- Title
- angular 9 change menu link item at runtime
- Category
- JavaScript
- Title
- inbound email sendgrid cloud functions
- Category
- JavaScript
- Title
- callback in response node.js
- Category
- JavaScript
- Title
- how to instialise var in js
- Category
- JavaScript
- Title
- how to create uuid in javascript
- Category
- JavaScript
- Title
- ad data to js object
- Category
- JavaScript
- Title
- how to auto refresh page in javascript
- Category
- JavaScript
- Title
- Angular Quick Tip: Binding Specific Keys to the Keyup and Keydown Events
- Category
- JavaScript
- Title
- convert json to csv npm
- Category
- JavaScript
- Title
- javascript check if variable is object
- Category
- JavaScript
- Title
- ajax file upload jquery
- Category
- JavaScript
- Title
- change bloodhound remote dynamically
- Category
- JavaScript
- Title
- discord.js reply to message
- Category
- JavaScript
- Title
- javascript after dom ready
- Category
- JavaScript
- Title
- find intersection between two object arrays javascript
- Category
- JavaScript
- Title
- function js
- Category
- JavaScript
- Title
- Getting the differences between two objects javascript lib
- Category
- JavaScript
- Title
- how to sepaarte text in object javascript
- Category
- JavaScript
- Title
- find the last occurrence of a character in a string javascript
- Category
- JavaScript
- Title
- angular date formats
- Category
- JavaScript
- Title
- dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.66.dylib Referenced from: /usr/local/bin/node Reason: image not found
- Category
- JavaScript
- Title
- how to validate the radio button using jquery
- Category
- JavaScript
- Title
- how to use chart js in vue js
- Category
- JavaScript
- Title
- how to pass a value to a react funtion without immediately firing it
- Category
- JavaScript
- Title
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- Category
- JavaScript
- Title
- credit card mask js
- Category
- JavaScript
- Title
- check items in array javascript
- Category
- JavaScript
- Title
- if statemnt shorthand js without else
- Category
- JavaScript
- Title
- discord.js bot github
- Category
- JavaScript
- Title
- event import in angular
- Category
- JavaScript
- Title
- angular file upload app with django
- Category
- JavaScript