返回除具有多个值的Object Array中第一次出现的所有重复项
我希望能够循环遍历对象数组并通过选择多个值进行检查来返回除对象的第一个实例之外的所有重复结果。
这是我的尝试:
const things = [ {
location: 'Lynnwood, Wa',
keyword: 'Video Production',
user: null,
profile: null,
id: '5c659a55783fad6667853dd4' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597e4783fad6667853d8b' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659288783fad6667853d86' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' } ];
function getDuplicates(arry) {
var duplicates = [];
arry.forEach((item, index) => {
if(arry.indexOf(item.location) != index && arry.indexOf(item.keyword) != index) {
duplicates.push(item);
}
});
return duplicates;
}
/*
Expected Result: (Excludes first occurance of dup but returns the rest)
[
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' }
]
*/
回答如下:
最直接的方法是使用一个数组,该数组包含您找到的所有唯一项,基本上用作一组。
关键是如果一个项目不在该集合中,则将其添加到该项目中,而不是添加到重复项目中(因为它是第一个且可能仅发生)。
.some数组方法实际上是用于深度对象比较的离合器(在这里表现类似于.include)。
考虑到这一点,下面的代码非常简单,可以使用其他数组方法或其他方法进行压缩/增强。
let uniqueItems = []
arry.forEach(item => {
if(uniqueItems.some(other =>
item.location === other.location
&& .... && ... && ....
)){
duplicates.push(item)
} else {
uniqueItems.push(item)
}
}
返回除具有多个值的Object Array中第一次出现的所有重复项
我希望能够循环遍历对象数组并通过选择多个值进行检查来返回除对象的第一个实例之外的所有重复结果。
这是我的尝试:
const things = [ {
location: 'Lynnwood, Wa',
keyword: 'Video Production',
user: null,
profile: null,
id: '5c659a55783fad6667853dd4' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597e4783fad6667853d8b' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659288783fad6667853d86' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' } ];
function getDuplicates(arry) {
var duplicates = [];
arry.forEach((item, index) => {
if(arry.indexOf(item.location) != index && arry.indexOf(item.keyword) != index) {
duplicates.push(item);
}
});
return duplicates;
}
/*
Expected Result: (Excludes first occurance of dup but returns the rest)
[
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' }
]
*/
回答如下:
最直接的方法是使用一个数组,该数组包含您找到的所有唯一项,基本上用作一组。
关键是如果一个项目不在该集合中,则将其添加到该项目中,而不是添加到重复项目中(因为它是第一个且可能仅发生)。
.some数组方法实际上是用于深度对象比较的离合器(在这里表现类似于.include)。
考虑到这一点,下面的代码非常简单,可以使用其他数组方法或其他方法进行压缩/增强。
let uniqueItems = []
arry.forEach(item => {
if(uniqueItems.some(other =>
item.location === other.location
&& .... && ... && ....
)){
duplicates.push(item)
} else {
uniqueItems.push(item)
}
}