数组去重切新数据覆盖旧数据
const dealRearrangement = (arr) => {
var obj = {};
arr = arr.reduce(function (item, next) {
if (obj[next.id]) {
item.map((i) => {
if (i.id === next.id) {
i.layerName = next.layerName;
i.layerPercent = next.layerPercent;
}
});
} else obj[next.id] = true && item.push(next);
return item;
}, []);
return arr;
};
const arr = [
{
id: "1",
layerName: "1",
layerPercent: "1",
},
{
id: "1",
layerName: "2",
layerPercent: "2",
},
{
id: "1",
layerName: "3",
layerPercent: "3",
},
{
id: "2",
layerName: "4",
layerPercent: "4",
},
];
console.log(dealRearrangement(arr));
## 希望内容对你有帮助,如果有错误请联系我 q: 1911509826,感谢支持