简体   繁体   English

在数组上创建多个对象循环

[英]Create multiple object looping over array

So i have an array which stores hobbies for each user in an array within the object.. 所以我有一个数组,它为对象内的数组中的每个用户存储爱好。

var hobbies = [
  {
    "id": 1,
    "hobbies": []
  },

  {
    "id": 2,
    "hobbies": [
      "football"
    ]
  },
  {
    "id": 3,
    "hobbies": [
      "football",
      "basketball"
    ]
  }
]

What i want to return is a new array of objects but each hobby separated into their own object like below. 我想要返回的是一个新的对象数组,但每个爱好分成他们自己的对象,如下所示。

var result = [
  {
    "id": 2,
    "hobby": "football"
  },
 {
    "id": 3,
    "hobby": "football"
  },
 {
    "id": 3,
    "hobby": "basketball"
  }
]

What is have so far is 到目前为止是什么

hobbies.filter((f, i) => f.hobbies.length > 0).map((p, i) => {
    while (i < p.hobbies.length) {
 return { id : p.id, hobby : p.hobbies[i] };
}
  });

which only returns 只返回

[
  {
    "id": 2,
    "hobby": "football"
  },
  {
    "id": 3,
    "hobby": "basketball"
  }
]

You can use array#reduce with array#map . 你可以使用array#reduce with array#map Iterate through each object and then iterate through each hobby of hobbies and create the object. 遍历每个对象,然后遍历每个爱好的hobbies并创建对象。

 var hobbies = [ { "id": 1, "hobbies": [] }, { "id": 2, "hobbies": [ "football" ] }, { "id": 3, "hobbies": [ "football", "basketball" ] } ], result = hobbies.reduce((r, {id, hobbies}) => r.concat(hobbies.map(hobby => ({id, hobby}))), []); console.log(result); 

I know, "functional" programming is considered "cool" around these parts, however, have you considered using simple loops to, well, loop over your data? 我知道,“功能”编程被认为是围绕这些部分的“酷”,但是,您是否考虑过使用简单的循环来循环数据?

let result = [];

for (let {hobbies, id} of data)
    for (let hobby of hobbies)
        result.push({id, hobby})

In my opinion, this is far more readable than any reduce spaghetti one could come up with ;) 在我看来,这比任何可以提出的reduce意大利面更具可读性;)

You need to use inner-loop to loop through the hobbies and push them one-by-one to the target array: 您需要使用内部循环来遍历业余爱好并将它们逐个推送到目标数组:

 var hobbies = [{ "id": 1, "hobbies": [] }, { "id": 2, "hobbies": [ "football" ] }, { "id": 3, "hobbies": [ "football", "basketball" ] } ]; var result = hobbies.reduce((acc, item) => { item.hobbies.forEach(hobby => { acc.push({ id: item.id, hobby: hobby }); }); return acc; }, []); console.log(result); 

You can use array.prototype.reduce : 您可以使用array.prototype.reduce

 var hobbies = [{"id": 1,"hobbies": []},{"id": 2,"hobbies": ["football"]},{"id": 3, "hobbies": ["football","basketball"]}]; var res = hobbies.reduce((m, o) => (o.hobbies.forEach(h => m.push({id: o.id, hobby: h})), m), []); console.log(res); 

You need nested loops and this is the basics of it: 你需要嵌套循环,这是它的基础:

You first need to loop over the main hobbies array. 首先需要遍历主要hobbies数组。

Then for each item in the array (which represents a person), you want to loop through their hobbies, and for each one of those hobbies, you need to push an object made up of the profile ID and the hobby into results array I created earlier. 然后对于数组中的每个项目(代表一个人),你想要遍历他们的爱好,并且对于每个爱好,你需要将由配置文件ID和爱好组成的对象推送到我创建的results数组中早。

 var hobbies = [{ "id": 1, "hobbies": [] }, { "id": 2, "hobbies": [ "football" ] }, { "id": 3, "hobbies": [ "football", "basketball" ] } ]; let result = []; hobbies.forEach(function(profile){ profile.hobbies.forEach(function(hobby){ result.push( { "id": profile.id, "hobby": hobby } ); }); }); console.log(result) 

Update: the other answers with Array.reduce (a more specialised loop) will cut the above code down even further. 更新:使用Array.reduce(一个更专业的循环)的其他答案将进一步削减上面的代码。

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

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