Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
Returning key values from an array of object Javascript

Hi I am 99% there with the code below but I need this array of objects to return:- 'Bear', 'Minu', 'Basil', 'Hamish' instead it returns Bear,MinuBasil,Hamish any ideas?

    gatherPets([{ name: 'Malcolm', pets: ['Bear', 'Minu'] },{ name: 'Caroline', pets: ['Basil', 
    'Hamish'] },]);

    function gatherPets(people) {
      let newArray=[];
      for (let i=0; i <=people.length; i++ ) {
        newArray = newArray +people[i].pets
      }
      return (newArray)
    }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I hope this code helping you

array.flatMap(o=>o.pets)
about 4 years ago · Juan Pablo Isaza Report

0

You could use the Array#flatMap method for this.

export function gatherPets(pople){
  return people.flatMap(p => p.pets);
}
about 4 years ago · Juan Pablo Isaza Report

0

try this

gatherPets([{ name: 'Malcolm', pets: ['Bear', 'Minu'] },{ name: 'Caroline', pets: ['Basil', 
'Hamish'] },]);

function gatherPets(people) {
let newArray=[];
for (let i=0; i <people.length; i++ ){
newArray.push(...people[i].pets);    
}
return (newArray)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!