0

I'm new to full-stack development, specifically to java-script and functional programming. Using high-order function (like map, filter and so) I need to change inner object array values into the outer array given value:

let arr = [
[ [ 
  {name: "Alice", age: 22},
  {name: "Charlie", age: 35}
 ], "Bob" ],
 [ [
  {name: "John" , age: 42}
  ], "Ben"]
]

and I need output array of:

output = [
 [ 
  {name: "Bob", age: 22},
  {name: "Bob", age: 35}
 ],
 [ 
  {name: "Ben", age: 42}
 ]
]

Thank you.

2 Answers 2

2

You already knew to use map, so here it is:

arr.map(x => x[0].map(y => {return {...y, name: x[1]}}))

For each item of the main list, we map it to a list of objects with the name replaced.

Sign up to request clarification or add additional context in comments.

Comments

0

Slightly modified Simon's answer. Added a little bit of destructuring sugar

let arr = [
[ [ 
  {name: "Alice", age: 22},
  {name: "Charlie", age: 35}
 ], "Bob" ],
 [ [
  {name: "John" , age: 42}
  ], "Ben"]
]

const result = arr.map(([data, name]) => data.map(({age}) => ({name, age})))

console.log(result)

Comments

Your Answer

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.