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

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

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)

Merge multiple object values of array into 1 [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 2 years ago.
This is my array:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3'
]
If want to check, if it has similar objects then I want it to be merged into one and return me array as:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
]
How is this possible in JavaScript or TypeScript?
Use Set to remove duplicate values :
array = [ 'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3' ];
var result = [...new Set(array)];
console.log(result);

Flatten array with arrays [duplicate]

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

How to combine 2 array to an Object? [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
Closed 8 years ago.
I have 2 arrays which are the same length like this:
$scope.access_name = ['group1','group2','group3','group4']
access_value=[1,2,1,3]
How can I combine to have result like this:
object_access = {'group1:1','group2:2','group3:1','group4:3'}
thankyou very much
var object_access={};
for(int i=0;i<access_value.length;i++)
{
object_access[$scope.access_name[i]]=access_value[i];
}

Categories