var allData = [ {key:1,value:2}, {key:2,value:3}, {key:1,value:2}, {key:1,value:3} ]; var result = []; var obj = {}; for(var h =0; h<allData.length; h++){ if(!obj[allData[h].key]){ result.push(allData[h]); obj[allData[h].key] = true; } } console.log(result);
结果如下:
-
- 0:{key: 1, value: 2}
- 1:{key: 2, value: 3}
- length:2
- __proto__:Array(0)