分组
const map = {};
const dest = [];
let result: any[];
const data = [
{ code: "aa", name: "王二" },
{ code: "aa", name: "王二" },
{ code: "aa", name: "王二" },
{ code: "aa", name: "王二" },
];
data.forEach(item => {
if (!map[item.code]) {
dest.push({
code: item.code,
lst: [item]
});
map[item.code] = item;
} else {
dest.forEach(it => {
if (it.code === item.code) {
it.lst.push(item);
}
});
}
});
result = [...dest];
复制代码
去重
//ES6
const dic = [1, 1, 3, 5];
const rlt = Array.from(new Set(dic));
//ES5
const arr = [];
if (dic.length > 0) {
dic.forEach(item => {
if (arr.findIndex(it => it === item)) {
arr.push(item);
}
})
}
console.log(arr);
复制代码