其他分享
首页 > 其他分享> > 对象数组去重

对象数组去重

作者:互联网

   //对象数组的去重
    function uniqObjInArray(objarray) {
      let len = objarray.length;
      let tempJson = {};
      let res = [];
      for (let i = 0; i < len; i++) {        //取出每一个对象   
        tempJson[JSON.stringify(objarray[i])] = true;
      }
      let keyItems = Object.keys(tempJson);
      for (let j = 0; j < keyItems.length; j++) {
        res.push(JSON.parse(keyItems[j]));
      }
      return res;
    }

  

let objArr = [
      {
        InvoiceItemCode: '2',
        InvoiceItemName: '年后',
        Amount: 0,
      },
      {
        InvoiceItemCode: '2',
        InvoiceItemName: '年后',
        Amount: 0,
      },
      {
        InvoiceItemCode: '3',
        InvoiceItemName: '年后333',
        Amount: 0,
      },
    ]

  

 let res3 = uniqObjInArray(objArr);
    console.log("res3 is ", res3);

  

标签:对象,res3,objarray,InvoiceItemCode,数组,res,tempJson,let
来源: https://www.cnblogs.com/LFxanla/p/12178922.html