//对象数组的去重 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);
原文地址:https://www.cnblogs.com/LFxanla/p/12178922.html
时间: 2024-10-10 01:50:10