如何 return 基于 es6 中对象的 id 的唯一对象数组?
How to return an array of unique objects based on the id of the object in es6?
Array(96) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
0: Object { id: 1, name: "PrimShal01", period: 3, … }
1: Object { id: 61, name: "TertDeep01", period: 1, … }
2: Object { id: 37, name: "SecoDeep01", period: 2, … }
3: Object { id: 49, name: "TertShal01", period: 1, … }
4: Object { id: 13, name: "PrimDeep01", period: 3, … }
5: Object { id: 61, name: "TertDeep01", period: 1, … }
当我尝试以下代码时,我只获得了唯一 ID,但我想要对象:
const uniques = [new Set(all_filter_ids.map(pos => pos.id))]
当我尝试下面的代码时,我得到的结果和以前一样:
const uniques = [new Set(all_filter_ids)]
将它们变成一个以ID为索引的Map(一个键只能存在一个对象),然后将Map的值转回数组。
const map = new Map(all_filter_ids.map(pos => [pos.id, pos]));
const uniques = [...map.values()];
另一种解决方案:
const arr = [{ id: 1, name: "PrimShal01", period: 3},{ id: 61, name: "TertDeep01", period: 1},{ id: 37, name: "SecoDeep01", period: 2},{ id: 49, name: "TertShal01", period: 1},{ id: 13, name: "PrimDeep01", period: 3},{ id: 61, name: "TertDeep01", period: 1}]
const result = Object.values(
arr.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {})
);
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
维护一个 set
用于跟踪 ID 并使用 filter
const uniq = (arr, track = new Set()) =>
arr.filter(({ id }) => (track.has(id) ? false : track.add(id)));
const arr = [
{ id: 1, name: "PrimShal01", period: 3 },
{ id: 61, name: "TertDeep01", period: 1 },
{ id: 37, name: "SecoDeep01", period: 2 },
{ id: 49, name: "TertShal01", period: 1 },
{ id: 13, name: "PrimDeep01", period: 3 },
{ id: 61, name: "TertDeep01", period: 1 },
];
console.log(uniq(arr))
Array(96) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
0: Object { id: 1, name: "PrimShal01", period: 3, … }
1: Object { id: 61, name: "TertDeep01", period: 1, … }
2: Object { id: 37, name: "SecoDeep01", period: 2, … }
3: Object { id: 49, name: "TertShal01", period: 1, … }
4: Object { id: 13, name: "PrimDeep01", period: 3, … }
5: Object { id: 61, name: "TertDeep01", period: 1, … }
当我尝试以下代码时,我只获得了唯一 ID,但我想要对象:
const uniques = [new Set(all_filter_ids.map(pos => pos.id))]
当我尝试下面的代码时,我得到的结果和以前一样:
const uniques = [new Set(all_filter_ids)]
将它们变成一个以ID为索引的Map(一个键只能存在一个对象),然后将Map的值转回数组。
const map = new Map(all_filter_ids.map(pos => [pos.id, pos]));
const uniques = [...map.values()];
另一种解决方案:
const arr = [{ id: 1, name: "PrimShal01", period: 3},{ id: 61, name: "TertDeep01", period: 1},{ id: 37, name: "SecoDeep01", period: 2},{ id: 49, name: "TertShal01", period: 1},{ id: 13, name: "PrimDeep01", period: 3},{ id: 61, name: "TertDeep01", period: 1}]
const result = Object.values(
arr.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {})
);
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
维护一个 set
用于跟踪 ID 并使用 filter
const uniq = (arr, track = new Set()) =>
arr.filter(({ id }) => (track.has(id) ? false : track.add(id)));
const arr = [
{ id: 1, name: "PrimShal01", period: 3 },
{ id: 61, name: "TertDeep01", period: 1 },
{ id: 37, name: "SecoDeep01", period: 2 },
{ id: 49, name: "TertShal01", period: 1 },
{ id: 13, name: "PrimDeep01", period: 3 },
{ id: 61, name: "TertDeep01", period: 1 },
];
console.log(uniq(arr))