js对象数组/字符串数组去重
1.根据对象某一key值去重:
1.保留前面的重复值
/**
* @description: 数组根据某一key值去重
* @param {*} arr
* @param {*} property
* @return {*}
* @author: Simoon.jia
*/
const unique = (arr, property) => {
let obj = {};
return arr.reduce(function (preValue, item) {
obj[item[property]] ? '' : (obj[item[property]] = true && preValue.push(item));
return preValue;
}, []);
};
2.后面的重复值覆盖前面的
/**
* @description: js数组根据某一key值去重,后面的重复值覆盖之前的
* @param {*} arr
* @param {*} property
* @return {*}
* @author: Simoon.jia
*/
const unique = (arr, property) => {
let obj = {};
return arr.reduce(function (preValue, item) {
obj[item[property]] = item;
return Object.values(obj);
}, []);
};
使用:
let newCabinetAlarm = unique(alarmList, "cabinetId")


浙公网安备 33010602011771号