简体   繁体   English

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

[英]How to add second array objects in first array object based on id value using angular6?

This is my code 这是我的代码

 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"}]; 

In the above categories array id value is existed in actions array. 在上述categories中, actions数组中存在数组ID值。 so i want to combine the two arrays into one array like the output as follows. 所以我想将两个数组合并为一个数组,如下所示。

Output:- 输出:-

 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"}]}] 

use the map function along with the filter 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) 

for each category find action elements and add then to category object 为每个类别找到操作元素,然后添加到类别对象

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

You can simply use Array.reduce() to create a map of actions ,group by category Id . 您可以简单地使用Array.reduce()创建操作映射,按category Id分组。 And than you can use Array.map() on the categories to get the desired result. 然后,您可以在类别上使用Array.map()获得所需的结果。 The overall time complexity of this solution will be O(n) . 该解决方案的总时间复杂度为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); 

   // 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)

I am using for loops working fine. 我使用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]); } } } 

Any other approach without using for loops? 还有其他没有使用for循环的方法吗?

暂无
暂无

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

相关问题 如何使用angular6根据字符串数组过滤对象数组 - How to filter the array of objects based on array of strings by using angular6 无法将第二个数组对象值添加到 javascript 中的第一个对象数组中 - Unable to add second array object value into first array of objects in javascript 如果第二个数组的项包含数组的第一个 object 的 id,则合并 Angular 8 中的两个对象数组 - Merge two array of objects in Angular 8 if the item of second array contains the id of the first object of array 通过比较属性将第二个数组与第一个数组合并,如果对象不是第二个数组的一部分,则将属性添加到第一个数组中的对象 - merge second array with first array by comparing properties and add properties to objects in first array if object is not part of second array 如何根据每个对象id为数组中的每个对象添加一个密钥对值? - How to add to each object in an array a key-pair value based on each objects id? 通过比较字段更新基于第二个数组的第一个数组对象值 - Update the first array object value based on the second array by comparing a field 如何根据对象数组中属性的第一个字符过滤对象数组? - How to filter an array of objects based on the first character of a property in the array of object? 对象数组 (JS) - 如何将第一个 object 的内容复制到第二个? - Array of objects (JS) - How to copy the content of the first object into the second one? 如何在对象数组中动态添加 object 值 - How to add object value dynamically in array of objects 如何为对象数组中的特定 object 添加值? - How to add a value to the specific object in the array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM