简体   繁体   English

从对象数组中,返回新数组中的键

[英]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: 我有一个对象数组,并且必须以新数组返回关键的puppies :此函数采用以下格式的狗数组:

[
  {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() : 另一个选择是使用Array.map()获得幼犬,并通过扩展到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. 您只需要reduce 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. Array.prototype.push会将每个参数推入数组,但不会推入每个参数的单个数组元素。

The easiest way to correct your code is to replace push with concat : 更正代码的最简单方法是用concat替换push

The concat() method is used to merge two or more arrays. concat()方法用于合并两个或多个数组。

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. 您需要遍历puppies数组,并将每个pupple分别添加到解决方案数组。 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. 第二个内部循环没有将'puppies'字段添加到解决方案数组,而是循环遍历每个对象的puppies数组并将其添加到solution数组。 The second inner loop can easily be done by calling forEach() on the puppies array. 通过在puppies数组上调用forEach()可以轻松完成第二个内部循环。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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