Typescript 按重复属性过滤对象数组
Typescript Filter Array of Objects by Duplicate Properties
我有一个对象数组,我想筛选出 return 个具有重复 属性 值的对象。
我的对象数组:
values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' }
];
我希望在新的对象数组中得到的结果:
duplicateValues = [
{ id: 1, name: 'a' },
{ id: 4, name: 'a' }
];
function findDuplicates(data) {
// map containing already added names.
const existing = new Map();
// set containing names, which were parsed in passed and added at future occurence of that name
const consideredReadd = new Set();
// final output array
const output = [];
data.forEach((item) => {
if(!existing.has(item.name)) {
// if not present in already parsed names, add in map
existing.set(item.name, item);
} else {
if (!consideredReadd.has(item.name)) {
// if is already present and past record isn't added in output array, add that record in output array and set the key in data set.
consideredReadd.add(item.name);
output.push(existing.get(item.name));
}
// push the current item as it existed in past
output.push(item);
}
});
// return output array
return output;
}
let values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' }
];
duplicateValues = findDuplicates(values);
console.log(duplicateValues);
- 使用 Array.prototype.reduce
按 name
对值进行分组
- 使用Array.prototype.filter过滤长度大于
1
的组。
- 最后使用 Array.prototype.flat.
展平结果数组
const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];
const duplicates = Object.values(
values.reduce((r, v) => ((r[v.name] ??= []).push(v), r), {})
)
.filter((g) => g.length > 1)
.flat();
console.log(duplicates);
TypeScript 解决方案:
type Values = { id: number, name: string }[];
const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];
const duplicates = Object.values(
values.reduce(
(r: { [k: string]: Values }, v) => ((r[v.name] ??= []).push(v), r),
{}
)
)
.filter((g) => g.length > 1)
.flat();
console.log(duplicates);
您可以复制数组并根据值过滤它们。在此处查看演示:https://stackblitz.com/edit/typescript-xww2ai?file=index.ts
const values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' },
];
const copyValues = [...values];
const duplicateObjects = values.filter(
(v) => copyValues.filter((cp) => cp.name === v.name).length > 1
);
console.log(duplicateObjects);
我有一个对象数组,我想筛选出 return 个具有重复 属性 值的对象。
我的对象数组:
values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' }
];
我希望在新的对象数组中得到的结果:
duplicateValues = [
{ id: 1, name: 'a' },
{ id: 4, name: 'a' }
];
function findDuplicates(data) {
// map containing already added names.
const existing = new Map();
// set containing names, which were parsed in passed and added at future occurence of that name
const consideredReadd = new Set();
// final output array
const output = [];
data.forEach((item) => {
if(!existing.has(item.name)) {
// if not present in already parsed names, add in map
existing.set(item.name, item);
} else {
if (!consideredReadd.has(item.name)) {
// if is already present and past record isn't added in output array, add that record in output array and set the key in data set.
consideredReadd.add(item.name);
output.push(existing.get(item.name));
}
// push the current item as it existed in past
output.push(item);
}
});
// return output array
return output;
}
let values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' }
];
duplicateValues = findDuplicates(values);
console.log(duplicateValues);
- 使用 Array.prototype.reduce 按
- 使用Array.prototype.filter过滤长度大于
1
的组。 - 最后使用 Array.prototype.flat. 展平结果数组
name
对值进行分组
const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];
const duplicates = Object.values(
values.reduce((r, v) => ((r[v.name] ??= []).push(v), r), {})
)
.filter((g) => g.length > 1)
.flat();
console.log(duplicates);
TypeScript 解决方案:
type Values = { id: number, name: string }[];
const values = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "a" }];
const duplicates = Object.values(
values.reduce(
(r: { [k: string]: Values }, v) => ((r[v.name] ??= []).push(v), r),
{}
)
)
.filter((g) => g.length > 1)
.flat();
console.log(duplicates);
您可以复制数组并根据值过滤它们。在此处查看演示:https://stackblitz.com/edit/typescript-xww2ai?file=index.ts
const values = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'a' },
];
const copyValues = [...values];
const duplicateObjects = values.filter(
(v) => copyValues.filter((cp) => cp.name === v.name).length > 1
);
console.log(duplicateObjects);