1、对数组对象中某个属性进行排序
plainOptions = plainOptions.map((items) => {
return {
text: items,
value: items,
};
});
function sortValue(a, b) {
return a.value - b.value;
}
plainOptions.sort(sortValue);
2、对数组对象中某个属性进行去重
1、
const map = new Map();
const newArr = sortedData.filter(
(v) => !map.has(v?.id) && map.set(v?.id, 1)
);
console.log(newArr);
2、
/**
* @description: 去除数值对象重复项
* @param {*} data 数据源
* @param {*} key 需要作比较的label值
* @return {*}
*/
const duplicateRemove = (data: any[], key: string | number): any => {
const temp = {};
let newData = [];
newData = data.reduce((pre: any[], cur: any) => {
if (!temp[cur[key]]) {
pre.push(cur);
temp[cur[key]] = true;
}
return pre;
}, []);
return newData;
};
duplicateRemove(data, 'key')