I have array which contains affiliate arrays, I don't want to access children of the array by index, my aim is to merge this data and and get the following result: [{id:'11223', price:92},{id:'92221', price:90}]
, What is the best way to achieve this? Thanks.
Sum of the final reulst:
let finalResult = [{id:'11223', price:92},{id:'92221', price:90}]
let sum = finalResult.reduce((acc, curr)=> {
return acc curr.price
}, 0)
console.log(sum)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
`
Nested array:
let nestedArray = [
[
{
id:'11223',
price:92
}
],
[
{
id:'92221',
price:90
}
]
]
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can simply flat
the nestedArray
as:
nestedArray.flat()
let nestedArray = [
[
{
id: "11223",
price: 92,
},
],
[
{
id: "92221",
price: 90,
},
],
];
const arr = nestedArray.flat();
console.log(arr);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>