【问题标题】:Remove all duplicate occurrences of JSON object array删除所有重复出现的 JSON 对象数组
【发布时间】:2020-04-13 11:49:32
【问题描述】:
originaData = [
{id: 1, description: abc },
{id: 2, description: def }
{id: 1, description: ghi }
{id: 3, description: jkl }
{id: 2, description: mno }
{id: 4, description: pqr }
]

expected result
uniqueData = [
{id: 3, description: jkl },
{id: 4, description: pqr }
]

duplicateData = [
{id: 1, description: abc },
{id: 2, description: def },
{id: 1, description: ghi },
{id: 2, description: mno }
]

我已经尝试过 lodash 的 uniqBy。但它并没有给我预期的结果,因为它没有删除所有重复的事件。

【问题讨论】:

    标签: javascript arrays typescript object


    【解决方案1】:

    您可以使用过滤器执行此操作,并在 vanilla javascript 中查找:

    originaData.filter((o, index) => originaData.find((_o, _index) => o.id === _o.id && index !== _index))

    以上将为您提供数组中的重复数据。

    要获得一个没有重复数据的数组,只需在函数逻辑中使用!

    originaData.filter((o, index) => !originaData.find((_o, _index) => o.id === _o.id && index !== _index))

    【讨论】:

      【解决方案2】:

      您可以按id 分组,并按数组长度对分组数据进行排序。

      var data = [{ id: 1, description: 'abc' }, { id: 2, description: 'def' }, { id: 1, description: 'ghi' }, { id: 3, description: 'jkl' }, { id: 2, description: 'mno' }, { id: 4, description: 'pqr' }],
          [duplicate, unique] = Object
              .values(data.reduce((r, o) => ((r[o.id] = r[o.id] || []).push(o), r), {}))
              .reduce((r, a) => (r[+(a.length === 1)].push(...a), r), [[], []]);        
      
      console.log(unique);
      console.log(duplicate);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案3】:

        您可以使用 Array.reduce 方法在原版 javascript 中实现此目的。首先按 id 对数据进行分组,然后将这些组分成唯一和重复的数组。如果您的组只有一个项目,则该项目是唯一的,或者如果该组有多个条目,则包含的条目是重复的。

        const originialData = [
          {id: 1, description: 'abc' },
          {id: 2, description: 'def' },
          {id: 1, description: 'ghi' },
          {id: 3, description: 'jkl' },
          {id: 2, description: 'mno' },
          {id: 4, description: 'pqr' }
        ]
        
        // Group data as an object by using the Array.reduce method.
        const groupedById = originialData.reduce((groups, item) => {
          // if a group has not yet been created, default to empty array.
          if (!groups[item.id]) {
            groups[item.id] = [];
          }
        
          // push the entry into the group
          groups[item.id].push(item);
        
          return groups;
        }, {});
        
        // Get the group keys
        const groupKeys = Object.keys(groupedById);
        
        // Iterate the group keys, and divide into unique and duplicates array
        const [unique, duplicates] = groupKeys.reduce(([uniq, dupl], id) => {
          // Get the current group
          const group = groupedById[id];
        
          // Select array depending on length
          if (group.length === 1) { // if length is 1, then the item is unique
            uniq = uniq.concat(group);
          } else { // else the containing items are duplicates.
            dupl = dupl.concat(group);
          }
        
        
          return [uniq, dupl];
        }, [[], []])
        
        console.log(unique)
        

        这里的工作示例:https://jsbin.com/sijidacile/edit?js,console

        【讨论】:

          【解决方案4】:

          您可以计算数组中的 id。然后根据 id 计数对数据进行分区。

          function tally(iterable, fn = obj => obj) {
            const tally = new Map();
            for (const item of iterable) {
              const key = fn(item);
              if (!tally.has(key)) tally.set(key, 0);
              tally.set(key, tally.get(key) + 1);
            }
            return tally;
          }
          
          function partition(iterable, fn = obj => obj) {
            const truthy = [], falsy = [];
            for (const item of iterable) {
              (fn(item) ? truthy : falsy).push(item);
            }
            return [truthy, falsy];
          }
          
          const originaData = [{id:1,description:'abc'},{id:2,description:'def'},{id:1,description:'ghi'},{id:3,description:'jkl'},{id:2,description:'mno'},{id:4,description:'pqr'}];
          const idCount = tally(originaData, obj => obj.id);
          const [uniqueData, duplicateData] =
            partition(originaData, obj => idCount.get(obj.id) == 1);
          
          console.log(uniqueData);
          console.log(duplicateData);

          如果您只需要两者之一(uniqueDataduplicateData),请改用 filter

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-11-03
            • 2022-01-20
            • 2018-06-19
            • 1970-01-01
            • 2011-06-24
            • 2021-10-23
            相关资源
            最近更新 更多