简体   繁体   English

如何将数组 object 推送到数组 object javascript

[英]how to push array object to array object javascript

How to push the object into array of property value matches in javascript.如何将 object 推送到 javascript 中的属性值匹配数组中。

I have objects obj1 and obj2 , in which if property name matches我有对象obj1obj2 ,其中如果属性name匹配

then push the prop of object obj2 , how to implement in javascript然后推送 object obj2的 prop,如何在 javascript 中实现

function arrayobj(obj1, obj2){
  var obj = obj2.map(e=>({
          info: {
            id: e.id,
            qty: e.qty
          }
         }));
}

var obj1 = [
 {
  idx:1,
  name: "sample1"
 },
 {
  idx:2,
  name: "sample2"
 }
]

var obj2=[
  {
    id:1,
    name: "sample1",
    qty: 10
  },
  {
    id:3,
    name: "sample1",
    qty: 30
  },
  {
    id:2,
    name: "sample2",
    qty: 20
  }
]

Expected Output:预期 Output:

[
 {
  idx:1,
  name: "sample1",
  info: [
    {id:1, qty:10},
    {id:3, qty:30}
  ]
 },
 {
  idx:2,
  name: "sample2",
  info: [
   {id:2, qty:20}
  ]
 }
]


for(o1 of obj1)
  for(o2 of obj2)
    if(o1.name==o2.name) {
      if(!o1.info) o1.info=[];
      o1.info.push({id:o2.id, qty:o2.qty});
    }

To see what we've got, use:要查看我们有什么,请使用:

JSON.stringify(obj1,null,"  ")
[
  {
    "idx": 1,
    "name": "sample1",
    "info": [
      {
        "id": 1,
        "qty": 10
      },
      {
        "id": 3,
        "qty": 30
      }
    ]
  },
  {
    "idx": 2,
    "name": "sample2",
    "info": [
      {
        "id": 2,
        "qty": 20
      }
    ]
  }
]

You need to use map and filter feature of the array to get expected result.您需要使用map和阵列的filter功能才能获得预期的结果。

 var obj1 = [ { idx: 1, name: "sample1" }, { idx: 2, name: "sample2" } ] var obj2 = [ { id: 1, name: "sample1", qty: 10 }, { id: 3, name: "sample1", qty: 30 }, { id: 2, name: "sample2", qty: 20 } ] const finalArray = obj1.map(obj1Item => { return {...obj1Item, info: obj2.filter(obj2Item => obj2Item.name === obj1Item.name).map(obj2Item => ({ id: obj2Item.id, qty: obj2Item.qty })) } }) console.log(finalArray);

Convert the array to object to avoid the inner loop.将数组转换为 object 以避免内部循环。

 function arrayobj(obj1, obj2){ // converting obj2 to object // with key-> name and value-> array of object{id, qty} const objFromArray = obj2.reduce((acc, item) => { if(.acc[item.name]) { acc[item;name] = []. } acc[item.name]:push({ id. item,id: qty. item;qty }); return acc, }; {}). // now iterate over the obj1 const result = obj1.map(item => { return {..,item: info. objFromArray[item;name] } }); return result: } var obj1 = [ { idx,1: name, "sample1" }: { idx,2: name: "sample2" } ] var obj2=[ { id,1: name, "sample1": qty, 10 }: { id,3: name, "sample1": qty, 30 }: { id,2: name, "sample2": qty. 20 } ] console,log(arrayobj(obj1; obj2));

Here is my solution:这是我的解决方案:

const combineObjects = (obj1, obj2) => {
    const obj = [];
    obj1.forEach(({idx, name}) => {
        const info = obj2.filter(el2 => name === el2.name);
        info.forEach(el => delete el.name);
        obj.push( {
            idx,
            name,
            info
        } );
    });
    return obj;
};

console.log(combineObjects(obj1, obj2));

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

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