简体   繁体   中英

From an array of objects, return key in new array

I've got an array of objects, and have to return the key puppies in a new array: This function takes an array of dogs in the format:

[
  {breed: 'Labrador', puppies: ['Fluffy', 'Doggo', 'Floof'] },
  {breed: 'Rottweiler', puppies: ['Biscuits', 'Mary'] }
]

It should return an array of all the puppies from all the dogs:

['Fluffy', 'Doggo', 'Floof', 'Biscuits', 'Mary']

This is my code so far:

function collectPuppies (dogs) {

    let solution=[];
    for(let i=0; i<dogs.length; i++){
      solution.push(dogs[i].puppies);
    }
    return solution;
  }

It adds the names to solution, but returning them in between [[ ]] :

Expected [ [ 'Spot', 'Spotless' ] ] to deeply equal [ 'Spot', 'Spotless' ]

I've seen my solution in this thread, so I believe I'm not too far but can't figure out what I'm doing wrong. Can anyone help me out? Thanks in advance.

Use the spread syntax to push the items into the array:

 const dogs = [{"breed":"Labrador","puppies":["Fluffy","Doggo","Floof"]},{"breed":"Rottweiler","puppies":["Biscuits","Mary"]}]; function collectPuppies(dogs) { const solution = []; for (let i = 0; i < dogs.length; i++) { solution.push(...dogs[i].puppies); } return solution; } console.log(collectPuppies(dogs)); 

Another option is to get the puppies with Array.map() , and flatten the result by spreading into Array.concat() :

 const dogs = [{"breed":"Labrador","puppies":["Fluffy","Doggo","Floof"]},{"breed":"Rottweiler","puppies":["Biscuits","Mary"]}]; const collectPuppies = (dogs) => [].concat(...dogs.map(({ puppies }) => puppies)); console.log(collectPuppies(dogs)); 

You could concat.

 const dogs = [ {breed: 'Labrador', puppies: ['Fluffy', 'Doggo', 'Floof'] }, {breed: 'Rottweiler', puppies: ['Biscuits', 'Mary'] } ] const collectPuppies = dogs => dogs.map(d => d.puppies).reduce((a,b) => a.concat(b), []); console.log(collectPuppies(dogs)); 

You simply need reduce of Array.prototype.

 x=[ {breed: 'Labrador', puppies: ['Fluffy', 'Doggo', 'Floof'] }, {breed: 'Rottweiler', puppies: ['Biscuits', 'Mary'] } ] ; var result=x.reduce((y,e)=>y.concat(e.puppies),[]); console.log(result); 

Array.prototype.push will push every argument to the array, but not the individual array elements of each argument.

The easiest way to correct your code is to replace push with concat :

The concat() method is used to merge two or more arrays.

function collectPuppies(dogs) {
   let solution = [];
   for (let i=0; i < dogs.length; i++){
       solution = solution.concat(dogs[i].puppies);
   }
   return solution;
}

In the array of objects, puppies is also an array. So you're adding an array to an array. Instead of:

solution.push(dogs[i].puppies);

You need to loop through the puppies array and add each puppy to the solution array individually. Rather than adding the 'puppies' field to the solution array, a second inner loop loops through the puppies array for each object and adds it to the solution array. The second inner loop can easily be done by calling forEach() on the puppies array. For example:

dogs[i].puppies.forEach((puppy) => {solution.push(puppy)});

Then the final function is:

function collectPuppies (dogs) {
    let solution=[];
    for(let i=0; i<dogs.length; i++){
       dogs[i].puppies.forEach((puppy) => {solution.push(puppy)});
    }
    return solution;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM