0

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)

`

Nested array:

let nestedArray = [
 [
 { 
 id:'11223',
 price:92
 }
 ],
 
 [
 {
 id:'92221',
 price:90
 }
 ]
]

asked Nov 14, 2021 at 15:11

1 Answer 1

1

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

answered Nov 14, 2021 at 15:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.