简体   繁体   English

在对象 JavaScript 中添加对象数组

[英]Adding array of object inside an object JavaScript

I have set of 2 data objects and they have one same element.我有一组 2 个数据对象,它们有一个相同的元素。 How do I merge theme into single array object?如何将主题合并为单个数组对象?

var itemList = [];

var categoryList = [{
      cat_id: 1,
      cat_name: 'category1'
   },
   {
      cat_id: 2,
      cat_name: 'category2'
   }
];

var productList = [{
      cat_id: 1,
      prod_code: 'a1',
      prod_name: 'product1'
   },
   {
      cat_id: 1,
      prod_code: 'a2',
      prod_name: 'product2'
   },
   {
      cat_id: 2,
      prod_code: 'b1',
      prod_name: 'product3'
   },
   {
      cat_id: 2,
      prod_code: 'b2',
      prod_name: 'product4'
   }
]

It will look like this它看起来像这样

itemList = [{
      cat_id: 1,
      cat_name: 'category1',
      products: [{
            prod_code: 'a1',
            prod_name: 'product1'
         },
         {
            prod_code: 'a2',
            prod_name: 'product2'
         }
      ]
   },
   {
      cat_id: 2,
      cat_name: 'category2',
      products: [{
            prod_code: 'b1',
            prod_name: 'product3'
         },
         {
            prod_code: 'b2',
            prod_name: 'product4'
         }
      ]
   }
]

I tried using itemList.push and I can't seem to get it to work.我尝试使用itemList.push并且我似乎无法让它工作。

let merged first two objects = {...obj1, ...obj2};让合并前两个对象 = {...obj1, ...obj2}; Then push that item into itemList array.然后将该项目推入 itemList 数组。 Maybe this can help you !!也许这可以帮助你!!

You can do the trick using map and filter您可以使用mapfilter

 var categoryList = [{ cat_id: 1, cat_name: 'category1' }, { cat_id: 2, cat_name: 'category2' } ]; var productList = [{ cat_id: 1, prod_code: 'a1', prod_name: 'product1' }, { cat_id: 1, prod_code: 'a2', prod_name: 'product2' }, { cat_id: 2, prod_code: 'b1', prod_name: 'product3' }, { cat_id: 2, prod_code: 'b2', prod_name: 'product4' } ]; var itemList = [...categoryList]; itemList.map(item => { item.products = productList.filter( pro => pro.cat_id === item.cat_id); }); console.log(itemList);

You can check my solution as below, here idea is to create a deep copy(so that categoryList will still remain the same) of the categoryList and then loop through both the lists and add the result into tempList.您可以按如下方式检查我的解决方案,这里的想法是创建 categoryList 的深层副本(以便 categoryList 仍将保持不变),然后遍历两个列表并将结果添加到 tempList 中。

var tempList = categoryList.map(a => Object.assign({}, a)); // just to do deep copy 

for( var i =0; i< tempList.length; i++) {
   var temp = [];
   for( var j =0; j< productList.length; j++){
       if(tempList[i]["cat_id"] == productList[j]["cat_id"]) {
           var obj = {};
           obj["prod_code"] = productList[j]["prod_code"];
           obj["prod_name"] = productList[j]["prod_name"];
           temp.push(obj);
       }
   }
   tempList[i]["products"] = temp;
}

console.log(JSON.stringify(tempList[0]));
console.log(JSON.stringify(tempList[1]));

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

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