Javascript 从包含对象的数组中删除重复项
我有这样的想法:Javascript 从包含对象的数组中删除重复项,javascript,arrays,angular,typescript,object,Javascript,Arrays,Angular,Typescript,Object,我有这样的想法: items: { _id: number; place: SomeAddressClassDto }[] = []; sessionItem: { _id: number; place: SomeAddressClassDto }; createAddressList() { this.service.getWorkingPlaces().subscribe((items) => { this.items = items;
items: { _id: number; place: SomeAddressClassDto }[] = [];
sessionItem: { _id: number; place: SomeAddressClassDto };
createAddressList() {
this.service.getWorkingPlaces().subscribe((items) => {
this.items = items;
this.sessionItem = {
place: JSON.parse(sessionStorage.getItem('currentPlace')),
_id: this.items.length,
};
this.items.push(this.sessionItem);
// _.uniqBy(this.items, 'place')
const idx = items.findIndex((item) => this.service.comparePlaces(item.place,
this.service.getCurrentWorkingPlace()));
if (idx !== -1) this.radiobox.option = `${idx}`;
});
}
0: place: {name, street, city, etc...}
_id: 0
_.uniqBy(this.items, (x) => x.place && x.place.someOtherPropertyInsideOfPlace);
我正试图使用uniqBy方法从“items”数组中删除任何重复项,但它不起作用。我想这是因为对于这个项目,_id总是不同的,但是items.place可以是相等的,如果是,我想摆脱这个
也许更好的方法是检查相同的项。place已经在该数组中,但不知道如何执行此操作
编辑:
更详细地说,项目如下所示:
items: { _id: number; place: SomeAddressClassDto }[] = [];
sessionItem: { _id: number; place: SomeAddressClassDto };
createAddressList() {
this.service.getWorkingPlaces().subscribe((items) => {
this.items = items;
this.sessionItem = {
place: JSON.parse(sessionStorage.getItem('currentPlace')),
_id: this.items.length,
};
this.items.push(this.sessionItem);
// _.uniqBy(this.items, 'place')
const idx = items.findIndex((item) => this.service.comparePlaces(item.place,
this.service.getCurrentWorkingPlace()));
if (idx !== -1) this.radiobox.option = `${idx}`;
});
}
0: place: {name, street, city, etc...}
_id: 0
_.uniqBy(this.items, (x) => x.place && x.place.someOtherPropertyInsideOfPlace);
因此,有可能有非常相似的对象,其中只有一个属性不同{}我建议使用
\u id
来确定该值是否唯一。如果使用place
则值可能总是唯一的,因为它们可能是同一类的不同实例。(我无法从代码中判断是否是这种情况)
或者,您可以使用place
内部的属性来确定它们是否是唯一的,如下所示:
items: { _id: number; place: SomeAddressClassDto }[] = [];
sessionItem: { _id: number; place: SomeAddressClassDto };
createAddressList() {
this.service.getWorkingPlaces().subscribe((items) => {
this.items = items;
this.sessionItem = {
place: JSON.parse(sessionStorage.getItem('currentPlace')),
_id: this.items.length,
};
this.items.push(this.sessionItem);
// _.uniqBy(this.items, 'place')
const idx = items.findIndex((item) => this.service.comparePlaces(item.place,
this.service.getCurrentWorkingPlace()));
if (idx !== -1) this.radiobox.option = `${idx}`;
});
}
0: place: {name, street, city, etc...}
_id: 0
_.uniqBy(this.items, (x) => x.place && x.place.someOtherPropertyInsideOfPlace);
您可以使用
Array.Prototype.reduce()
和Array.Prototype.find()
基于place
属性创建一个没有重复项的数组
例如:
const test = [
{ _id: 1, place: 'a' },
{ _id: 2, place: 'a' },
{ _id: 3, place: 'a' },
{ _id: 4, place: 'b' },
{ _id: 5, place: 'c' },
]
const noDups = test.reduce((accum, current)=> {
if(!accum.find(item => item.place === current.place))
accum.push(current)
return accum
}, [])
console.log(noDups)
reduce()
将创建一个新数组(在本例中,acum
变量将被初始化为空数组),但只有在.place
属性唯一的情况下,才能在新数组中添加新项,前提是它尚未找到具有相同.place
属性的项
var-arr=[{
_id:1,
地点:“巴黎”
},
{
_id:2,
地点:东京
},
{
_id:3,
地点:安大略省
},
{
_id:4,
地点:“巴黎”
},
{
_id:5,
地点:“卡拉奇”
},
{
_id:6,
地点:“巴黎”
},
{
_id:7,
地点:“纽约”
}
];
var newArray=arr.filter((self、item、index)=>index.findIndex(t=>(t.place==self.place))==item);
log(newArray)代码>