Problem:
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)
Solution:
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.
const people = [
{
name: "mike",
jobs: ["waiter", "doctor"]
},
{
name: "nils",
jobs: ["singer", "actor"]
},
{
name: "nils",
jobs: ["nurse", "actor"]
}
];
const iterate = [].concat(...people.map(person => person.jobs));
console.log(iterate);