In the below examples, why am I not getting any value?
const people = [{
name: "mike",
jobs: ["waiter", "doctor"]
},
{
name: "nils",
jobs: ["singer", "actor"]
},
{
name: "nils",
jobs: ["nurse", "actor"]
},
]
const iterate = people.map(person => {
return person.jobs.map(job => {
return job;
})
return res;
})
console.log(iterate)
const people = [{
name: "mike",
jobs: ["waiter", "doctor"]
},
{
name: "nils",
jobs: ["singer", "actor"]
},
{
name: "nils",
jobs: ["nurse", "actor"]
},
]
let res = []
const iterate = people.map(person => {
return person.jobs.map(job => {
res.push(job)
})
return res;
})
console.log(iterate)
4
Answers
The code reformats the data from an array of objects into a new array of arrays. Each inner array now contains the jobs of a specific person.
Another approach using
.flat(Infinity)
method:Thanks to @pilchard for the suggestion
flatMap
: