返回只有重复数组的数组的有效方法是什么?
array = [
{ name: 'name1', description:'desc1', place:'place1' },
{ name: 'name1', description:'desc1', place:'place2' },
{ name: 'name3', description:'desc3', place:'place3' },
{ name: 'name4', description:'desc4', place:'place4' },
{ name: 'name4', description:'desc4', place:'place4' },
{ name: 'name5', description:'desc5', place:'place5' }
]
在这种情况下,我们只需要检查重复项的名称和描述属性。 结果应该返回:
duplicates = [
{ name: 'name1', description:'desc1', place:'place1' },
{ name: 'name4', description:'desc4', place:'place4' },
]
这样做的简单方法是什么?
答案 0 :(得分:3)
Filter数组,并使用Set检查name
和description
是否已经出现,如果是,则添加到输出中。如果没有添加到Set:
const array = [{"name":"name1","description":"desc1","place":"place1"},{"name":"name1","description":"desc1","place":"place2"},{"name":"name3","description":"desc3","place":"place3"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name5","description":"desc5","place":"place5"}]
const result = array.filter(function({ name, description }) {
const key = `${name}-${description}`
return this.has(key) || !this.add(key)
}, new Set())
console.log(result)
要仅显示每个name
和description
组的一个副本,请改用Map,并计算出现次数。仅在数字为2时显示。
const array = [{"name":"name1","description":"desc1","place":"place1"},{"name":"name1","description":"desc1","place":"place2"},{"name":"name3","description":"desc3","place":"place3"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name5","description":"desc5","place":"place5"}]
const result = array.filter(function({ name, description }) {
const key = `${name}-${description}`
const status = (this.get(key) || 0) + 1
this.set(key, status)
return status === 2
}, new Map())
console.log(result)
基于Map解决方案的IE11解决方案:
const array = [{"name":"name1","description":"desc1","place":"place1"},{"name":"name1","description":"desc1","place":"place2"},{"name":"name3","description":"desc3","place":"place3"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name4","description":"desc4","place":"place4"},{"name":"name5","description":"desc5","place":"place5"}]
const result = array.filter(function(o) {
const key = o.name + '-' + o.description
const status = (this.get(key) || 0) + 1
this.set(key, status)
return status === 2
}, new Map())
console.log(result)
答案 1 :(得分:0)
这适用于2的重复。
let array = [
{ name: 'name1', description:'desc1', place:'place1' },
{ name: 'name1', description:'desc1', place:'place2' },
{ name: 'name3', description:'desc3', place:'place3' },
{ name: 'name4', description:'desc4', place:'place4' },
{ name: 'name4', description:'desc4', place:'place4' },
{ name: 'name5', description:'desc5', place:'place5' }
]
let duplicates = array.reverse()
.filter((el,index)=> {
let isDuplicate = index !== array.findIndex(({name,description})=> el.name === name && el.description === description);
return isDuplicate});
console.log(duplicates)