如何使用展开运算符从对象数组中删除重复项

15

我有一个以下的对象数组,其中id是唯一键:

var test = [
  {id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
  {id: 2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},
  {id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
  {id: 3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}
]

我想使用扩展运算符检索唯一对象,我尝试使用以下代码:

const uniKeys = [...(new Set(test.map(({ id }) => id)))];

我只能检索到 id,如何使用扩展运算符检索唯一对象。另外,任何新的 ES6 特性实现都将有所帮助。


这个线程:https://gist.github.com/telekosmos/3b62a31a5c43f40849bb 包含了从数组中获取唯一值的多种方式,包括针对对象数组(即基于某个键的唯一值筛选对象数组)。 - Jayce444
或许有帮助 - https://dev59.com/TlQJ5IYBdhLWcg3wlm7B#69815213 - Nijat Aliyev
5个回答

17
你可以使用find方法将其映射回对象数组,这将返回该id的第一个对象。

var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}]
var uniq = [...new Set(test.map(({id}) => id))].map(e => test.find(({id}) => id == e)); 
console.log(uniq)

你也可以使用 filter 方法。

var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}]

var uniq = test.filter(function({id}) {
  return !this[id] && (this[id] = id)
}, {})
 
console.log(uniq)


当我尝试第一个解决方案时,它会显示“类型 'Set<any>' 不是数组类型。使用编译器选项 '--downlevelIteration' 允许迭代器的迭代。” - tracer
尝试使用Array.from代替[...] - Nenad Vracar

6
您可以使用一个 Set 并通过未知的 id 进行过滤。

var test = [{ id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: "" }, { id: 2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode: "" }, { id: 1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: "" }, { id: 3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: "" }],
    unique = test.filter((s => ({ id }) => !s.has(id) && s.add(id))(new Set));
    
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }


4
您可以通过id创建一个Map,然后提取值。 [...new Map(test.map(item => [item.id, item])).values()]

var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},
{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}]

console.log([
  ...new Map(test.map(item => [item.id, item])).values()
])


当我尝试这个时,它会抛出“类型为'any[][]'的参数无法分配给类型为'Iterable <[{}, {}]>'的参数”错误。 - tracer

2

var test = [{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
{id:2, PlaceRef: "*00022", Component: "BAXI10R", SubLocCode: "KIT", BarCode:""},
{id:1, PlaceRef: "*00011", Component: "BATH", SubLocCode: "BAT", BarCode: ""},
{id:3, PlaceRef: "*00011", Component: "ANR190", SubLocCode: "B1", BarCode: ""}];


var uniqArray = Array.from(new Map(test.map(e=>[e.id, e])).values());
console.log(uniqArray)


0
使用 Lodash 工具库,您可以实现此操作。 let result = _.uniqBy(test, 'id');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接