Flatten array with arrays [duplicate] - javascript

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], []);

Related

How to deconstruct arrays of arrays in Javascript [duplicate]

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.

joining arrays with concat and using a loop [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
flatten an array without using .flat();
(16 answers)
Closed 9 months ago.
I run into the following problem, the following array: [[1,2,3],[4,5,6],[7,8,9]] I must go through it and get the following result [1, 2,3,4,5,6,7,8,9]. Currently I get the same result. Here my code:
let main= [[1,2,3],[4,5,6],[7,8,9]]
let result= [].concat(
main.map((e)=>{
return e
})
)
console.log(result)

How to concatenate and use spread operator in dart [duplicate]

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

Trying to concat an array of multiple array into one array [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
How to flatten nested array in javascript? [duplicate]
(27 answers)
Closed 2 years ago.
I try to concat Array of multiple Arrays like this
`let soldNumber = [
[1,2],
[5,7],
[35,67],
...
]`
to recieve this let soldNumber = [1,2,5,7,35,67,...]
soldNumber.flat()
Array.prototype.flat()
let soldNumber = [[1,2], [5,7], [35,67]];
let newSoldNumber = soldNumber.flat();

Comparing two arrays and creating another array if the items not found [duplicate]

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));

Categories