【问题标题】:how to push array object to array object javascript如何将数组对象推送到数组对象javascript
【发布时间】:2020-06-22 08:24:40
【问题描述】:

如何将对象推送到 javascript 中的属性值匹配数组中。

我有对象 obj1obj2,其中 if 属性 name 匹配

然后push对象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
  }
]

预期输出:

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


【问题讨论】:

    标签: javascript arrays object


    【解决方案1】:
    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});
        }
    

    要查看我们有什么,请使用:

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

    【讨论】:

    • 如果你喜欢我的回答,请给它投票,如果你认为它是最好的,请选择它。谢谢!
    【解决方案2】:

    您需要使用数组的mapfilter 功能才能获得预期的结果。

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

    【讨论】:

      【解决方案3】:

      将数组转换为对象以避免内部循环。

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

      【讨论】:

        【解决方案4】:

        这是我的解决方案:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-13
          • 1970-01-01
          • 2020-02-03
          • 2019-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多