This question already has answers here:
JavaScript flattening an array of arrays of objects
(14 answers)
Closed 4 months ago.
I'm working with an API and after I try to clean up the data, I got an array of arrays of arrays:
arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]]
How can I clean this up to something like this:
arr = [{name: "john"},{name: "jack"},{name: "joe"},{name: "bob"}]
You can use Array.prototype.flat(), providing Infinity as the depth argument to flatten all sub-arrays recursively:
const arr = [[[{name: "john"}],[{name: "jack"}]],[[{name: "joe"}],[{name: "bob"}]]];
const flattened = arr.flat(Infinity);
console.log(flattened);
In this case calling arr.flat().flat() would do the trick.
Related
This question already has answers here:
How to flatten a List?
(6 answers)
Closed 2 years ago.
This is js code of doing it!
nums = [].concat(...digitBuckets);
how can i implement it in dart?
nums=[].addAll(...digitBucketsd);//facing problem here and confused
The spread operator is designed to insert array elements into another array or to map its elements to function arguments.
The mistake is that: elements of array are used as arguments of concat function, but concat function requires array as argument but not its elements as arguments:
replace
nums = [].concat(...digitBuckets);
nums = [].addAll(...digitBucketsd);
with
nums = [].concat(digitBuckets);
nums = [].addAll(digitBucketsd);
or with spread
nums = [...digitBuckets];
nums = [...digitBucketsd];
also digitBucketsd is present in question instead of digitBuckets
This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
Say I have an array with arrays inside it. I would like to flatten it and get a single array with all the values.
let arrWithArrs = [[1,2,3], [4,5,6]];
let array = arrWithArrs.map(arr => ...arr);
This obviously doesn't work but I would like to know how to make it work.
The wanted outcome would be
array = [1,2,3,4,5,6];
You can perform a reduce on the array to combine each of the arrays into a single array like so:
arrWithArrs.reduce((result, lst) => [...result, ...lst], []);
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 3 years ago.
In javascript, I simply need to compare:
Array2 = ['a', 'd']
with
Array1=['a','b','c','d','e'] //full subset
and return a new array of items which are not in Array 1. so the result should be
Array3 = ['b','c','e']
Appreciate a quick reply. Many thanks in advance
Use a combinations of filter and includes likes so:
let newArray = Array1.filter(x => !Array2.includes(x));
This question already has answers here:
How can I reverse an array in JavaScript without using libraries?
(36 answers)
Closed 7 years ago.
I have an array as:
var arr = [1,2,3,4]
//Push and element to array
arr.push(5)
//Now, arr = [1,2,3,4,5]
I need to display my array as
Elements in array arr is:
5,1,2,3,4
Arr.reverse() gives me
5,4,3,2,1.
But i need
5,1,2,3,4
Simply use Array.prototype.reverse():
console.log(arr.reverse());
References:
Array.prototype.reverse().
This is the code you need to use :
arr.reverse();
Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to sort an array of javascript objects?
i have a array of objects:
var arr = []
arr[0] = new Object()
...
arr[n] = new Object()
I want to get sorted array by arr[i].getSortOrder() for example, where arr[i].getSortOrder() return integer value. How to do it ?
Just for the record:
arr.sort(function(a, b){return a.getSortOrder() - b.getSortOrder();});
For details see Sorting an array of JavaScript objects and the MDN docs for the sort() method.