-1

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)

3
  • Looks like return statement is missing in you map function Commented Oct 20, 2023 at 17:57
  • 1
    The first example is working (though possibly not as you intended it to). In your second example you're pushing results into res (and are not returning anything, resulting in those undefineds in the returned mapped array) but are logging iterate; I suspect you meant console.log(res) there. Commented Oct 20, 2023 at 17:59
  • could you explain to me why in exampe 1 I am returning the arrays and not the values inside? Also, is there a way I can do it without pushing into res and returning it? Commented Oct 20, 2023 at 18:12

4 Answers 4

1
  • I don't know much fancy way of writing this, but here is my solution.
  • You want to extracts jobs from people array of object.
  • What I did below is just created one variable to store job values.
  • Loop thour array of object and looped though jobs key value of each person and pushed it inside that array we have created.

const people = [{
    name: "mike",
    jobs: ["waiter", "doctor"]
  },
  {
    name: "nils",
    jobs: ["singer", "actor"]
  },
  {
    name: "nils",
    jobs: ["nurse", "actor"]
  },
]

const jobs = [];
people.forEach(person => {
  person.jobs.forEach(job => {
    jobs.push(job);
  })
})
console.log(jobs)

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

Comments

0

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

Comments

0

Another approach using .flat(Infinity) method:

const people = [
  {
    name: "mike",
    jobs: ["waiter", "doctor"]
  },
  {
    name: "nils",
    jobs: ["singer", "actor"]
  },
  {
    name: "nils",
    jobs: ["nurse", "actor"]
  }
];

const iterate = people.map(person => person.jobs).flat(Infinity)
console.log(iterate);

1 Comment

just flatMap you don't need Infinity just a single level of flattening. Also why did you post two answers?
-2

Thanks to @pilchard for the suggestion flatMap:

const people = [
  {
    name: "mike",
    jobs: ["waiter", "doctor"]
  },
  {
    name: "nils",
    jobs: ["singer", "actor"]
  },
  {
    name: "nils",
    jobs: ["nurse", "actor"]
  }
];
const iterate = people.flatMap(person => person.jobs)
console.log(iterate);

1 Comment

It is a duplicate vote to close. Also if you have more than one suggestion simply put them in one answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.