如何使用angular6在基于id值的第一个数组对象中添加第二个数组对象?

时间:2018-09-20 06:05:29

标签: javascript angular

这是我的代码

categories = [{"id":"101","name":"category1"},{"id":"102","name":"category2"},{"id":"103","name":"category3"},{"id":"104","name":"category4"}];

actions = [{"id":"201","name":"action1","category_id":"101"},{"id":"202","name":"action2","category_id":"101"},{"id":"203","name":"action3","category_id":"102"},{"id":"204","name":"action4","category_id":"104"}];

在上述categories数组中,id值存在于actions数组中。所以我想将两个数组组合成一个数组,如下所示。

输出:-

finalList = [{"id":"101","name":"category1","actions":[{"id":"201","name":"action1","category_id":"101"},{"id":"202","name":"action2","category_id":"101"}]},{"id":"102","name":"category2","actions":[{"id":"203","name":"action3","category_id":"102"}]},{"id":"103","name":"category3","actions":[]},{"id":"104","name":"category4","actions":[{"id":"204","name":"action4","category_id":"104"}]}]

5 个答案:

答案 0 :(得分:1)

将地图功能与filter

一起使用

var categories = [{"id":"101","name":"category1"},{"id":"102","name":"category2"},{"id":"103","name":"category3"},{"id":"104","name":"category4"}];

var actions = [{"id":"201","name":"action1","category_id":"101"},{"id":"202","name":"action2","category_id":"101"},{"id":"203","name":"action3","category_id":"102"},{"id":"204","name":"action4","category_id":"104"}];

var result = categories.map((item) => {
  item.action = actions.filter( ac => item.id === ac. category_id)
  return item;
})

console.log(result)

答案 1 :(得分:1)

为每个类别找到操作元素,然后将其添加到类别对象

this.categories.forEach((element) => {
    element['actions'] = this.actions.filter((data) => data.category_id === element.id);
});
console.log(this.categories);

答案 2 :(得分:0)

您可以简单地使用Array.reduce()创建一个动作图,按category Id分组。然后,您可以在类别上使用Array.map()以获得所需的结果。此解决方案的总体时间复杂度为O(n)

let categories = [{"id":"101","name":"category1"},{"id":"102","name":"category2"},{"id":"103","name":"category3"},{"id":"104","name":"category4"}];

let actions = [{"id":"201","name":"action1","category_id":"101"},{"id":"202","name":"action2","category_id":"101"},{"id":"203","name":"action3","category_id":"102"},{"id":"204","name":"action4","category_id":"104"}];

let map = actions.reduce((a,curr)=>{
  (a[curr.category_id]  = a[curr.category_id] || []).push(curr);
  return a;
},{});

let result = categories.map((o)=>{
  o.actions = map[o.id] || [];
  return o;
});

console.log(result);

答案 3 :(得分:0)

   // Use simple for loop along with filter 
categories = [{"id":"101","name":"category1"},{"id":"102","name":"category2"},{"id":"103","name":"category3"},{"id":"104","name":"category4"}];

actions = [{"id":"201","name":"action1","category_id":"101"},{"id":"202","name":"action2","category_id":"101"},{"id":"203","name":"action3","category_id":"102"},{"id":"204","name":"action4","category_id":"104"}];

    for(var i=0;i<categories.length;i++){
        categories[i]['actions'] = actions.filter((data) => data.category_id === categories[i].id);
    };
    console.log(categories)

答案 4 :(得分:0)

我正在使用for循环正常工作。

for(var i=0;i<categories.length;i++)
{
   categories[i]["actions"]=[];
   for(var j=0;j<actions.length;j++)
   {
     if(categories[i].id==actions[j].category_id){
       categories[i]["actions"].push(actions[j]);
     }
   }
}

还有其他不使用for循环的方法吗?