【问题标题】:Return all Duplicates except first occurrence in Object Array with multiple values返回具有多个值的对象数组中除第一次出现之外的所有重复项
【发布时间】:2019-02-14 18:02:25
【问题描述】:

我希望能够通过选择多个值来循环遍历对象数组并返回所有重复的结果,不包括对象的第一个实例。

这是我的尝试:

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' }
   ]
   */

【问题讨论】:

    标签: javascript node.js algorithm


    【解决方案1】:

    最直接的方法是使用一个数组,其中包含您找到的所有独特项目,基本上作为一个集合运行。

    关键是,如果一个项目不在该集合中,则将其添加到其中,而不是添加到重复项中(因为它是第一次也可能是唯一一次出现)。

    .some 数组方法真正用于深度对象比较(这里有点像 .includes)。

    考虑到这一点,下面的代码非常简单,绝对可以使用其他数组方法或其他方法进行压缩/增强。

    let uniqueItems = []
    arry.forEach(item => {
      if(uniqueItems.some(other => 
            item.location === other.location
            && .... && ... && ....
            )){
           duplicates.push(item)
      } else {
           uniqueItems.push(item)
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用数组的副本,通过使用Array#slice

      slice() 方法将数组的一部分的浅拷贝返回到从开始到结束(不包括结束)选择的新数组对象中。原数组不会被修改。

      function getDuplicates(arry) {
          ...
          arry.slice(1, ).forEach((item, index) => {
          ...
              }
          });
      
          return duplicates;
      }
      

      【讨论】:

        【解决方案3】:

        您可以将forEach 与数组和对象一起使用。

        数组来保持所需的输出,对象来跟踪元素的第一次出现。

        const things =[{location:'Lynnwood,Wa',keyword:'VideoProduction',user:null,profile:null,id:'5c659a55783fad6667853dd4'},{location:'LasVegas',keyword:'Cleaners',user:null,profile:null,id:'5c6597e4783fad6667853d8b'},{location:'LasVegas',keyword:'Cleaners',user:null,profile:null,id:'5c6597cc783fad6667853d8a'},{location:'LasVegas',keyword:'Cleaners',user:null,profile:null,id:'5c6597c7783fad6667853d89'},{location:'LasVegas',keyword:'Cleaners',user:null,profile:null,id:'5c6597c2783fad6667853d88'},{location:'LasVegas',keyword:'Cleaners',user:null,profile:null,id:'5c6597c2783fad6667853d87'},{location:'Lagos,La',keyword:'TopHome',user:null,profile:null,id:'5c659288783fad6667853d86'},{location:'Lagos,La',keyword:'TopHome',user:null,profile:null,id:'5c659219783fad6667853d84'},{location:'Lagos,La',keyword:'TopHome',user:null,profile:null,id:'5c659218783fad6667853d83'},{location:'Lagos,La',keyword:'TopHome',user:null,profile:null,id:'5c659218783fad6667853d82'}];
        
        let desired = []
        let included = {}
        
        things.forEach((inp)=>{
          if(included[inp.keyword]){
            desired.push(inp)
          } else {
            included[inp.keyword] = true
          }
         
        })
        
        console.log(desired)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-27
          • 2021-01-28
          • 1970-01-01
          • 2014-08-21
          • 2020-12-29
          • 2021-07-24
          • 2021-11-12
          相关资源
          最近更新 更多