对象数组去重
Posted lfxanla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象数组去重相关的知识,希望对你有一定的参考价值。
//对象数组的去重 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);
以上是关于对象数组去重的主要内容,如果未能解决你的问题,请参考以下文章