【问题标题】:json data check if same id repeatjson数据检查相同的id是否重复
【发布时间】:2018-09-06 14:14:28
【问题描述】:

我有一个 json 数据,其中一个 id 有不同的 color_id。 所以从那里我只想检查相同的 id 是否重复然后只保留第一个

这是我的 JSON 示例

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" },
    { "id": "1", "name": "yyy", "age": "15","color_id": "1" },
    { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

我想要的输出

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" },
    { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

我尝试了 reduce,但在那里我发现修改了数据结构,所以我不确定我是否会得到我想要的输出。

【问题讨论】:

  • 您想如何决定保留哪些记录以及删除哪些记录?
  • @PavelMolchanov 只想保留唯一的 ID。不重复相同的ID。其他值无关紧要。
  • @LemonKazi 看来您的“id”不是主键。如果您可以在端点本身上更改它会更有效。话虽如此,有一些方法可以删除 id 匹配的项目,但是您将如何定义细节?您是否尝试保留所有带有 color_id == 22 的项目,或确保唯一的 ID,但如果发生冲突,我将如何确定保留哪些?那么它会是数组中的第一个实例wins,还是特定的color_id,还是什么?
  • @Fallenreaper 我可以理解,出于这个原因,我只需要 id 其他不需要保留的数据。我最终过滤了数据。

标签: javascript jquery arrays json filter


【解决方案1】:

var data = [{
    "id": "1",
    "name": "xxx",
    "age": "22",
    "color_id": "22"
  },
  {
    "id": "1",
    "name": "yyy",
    "age": "15",
    "color_id": "1"
  },
  {
    "id": "5",
    "name": "zzz",
    "age": "59",
    "color_id": "22"
  }
];

let map = {};
let uniqueEntries = data.filter((el) => map[el.id] ? false : map[el.id] = true);
console.log(uniqueEntries )

说明:

  1. 您创建了一个我们存储 ID 的地图。
  2. 然后过滤数组,每次我们找到不在地图中的条目时,我们将其添加到列表中并返回 true。如果我们已经在列表中有它,我们返回 false 以丢弃该条目。

条件的最后一部分是使用赋值返回赋值的事实。

【讨论】:

    【解决方案2】:

    您可以使用reduce 创建一个新数组,并在这个新数组中使用findIndex 检查这个新数组是否有一个具有相同ID 的对象。如果存在具有相同 id 的对象,则不要推送另一个具有相同 id 的对象

    var data = [{
        "id": "1",
        "name": "xxx",
        "age": "22",
        "color_id": "22"
      },
      {
        "id": "1",
        "name": "yyy",
        "age": "15",
        "color_id": "1"
      },
      {
        "id": "5",
        "name": "zzz",
        "age": "59",
        "color_id": "22"
      }
    ];
    let m = data.reduce(function(acc, curr) {
      let findIndex = acc.findIndex(function(item) {
        return item.id === curr.id
      })
      if (findIndex === -1) {
        acc.push(curr)
    
      }
      return acc;
    }, [])
    
    console.log(m)

    【讨论】:

      【解决方案3】:

      使用Array.reduceArray.some

      const data = [{
          id: '1',
          name: 'xxx',
          age: '22',
          color_id: '22',
        },
        {
          id: '1',
          name: 'yyy',
          age: '15',
          color_id: '1',
        },
        {
          id: '5',
          name: 'zzz',
          age: '59',
          color_id: '22',
        },
      ];
      
      const reduced = data.reduce((tmp, x) => {
        if (tmp.some(y => y.id === x.id)) return tmp;
      
        return [
          ...tmp,
      
          x,
        ];
      }, []);
      
      console.log(reduced);

      或者Array.filter,因为这是来自@JGoodgive 的好主意,但有点不同

          const data = [{
              id: '1',
              name: 'xxx',
              age: '22',
              color_id: '22',
            },
            {
              id: '1',
              name: 'yyy',
              age: '15',
              color_id: '1',
            },
            {
              id: '5',
              name: 'zzz',
              age: '59',
              color_id: '22',
            },
          ];
      
          const reduced = data.filter((x, xi) => !data.slice(0, xi).some(y => y.id === x.id));
      
          console.log(reduced);

      【讨论】:

        【解决方案4】:

        您可以使用Set 来跟踪已处理的 ID。

        const
          // The data set with non-unique IDs
          data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" }, { "id": "1", "name": "yyy", "age": "15","color_id": "1" }, { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];
          
        function dedupe(items) {
          // Create a set to keep track of IDs already encountered.
          const
            idSet = new Set();
          // Filter the items, when an ID isn't in the set add it to the set and return true
          // so item is in the result array. When the ID is in the set return false so the 
          // item will be dropped.
          return items.filter(item => {
            // If the ID is already in the set, drop it from the result. This way only the
            // first item with an ID is added to the result.
            if (idSet.has(item.id)) {
              return false;
            }
            
            // Add the ID to the set, this way we keep track of the IDs already encountered.
            idSet.add(item.id);
            // Return true so the item is included in the result array.
            return true;
          });
        }    
        
        console.log(dedupe(data));

        【讨论】:

          猜你喜欢
          • 2019-01-23
          • 1970-01-01
          • 1970-01-01
          • 2016-02-13
          • 1970-01-01
          • 2019-06-19
          • 2019-07-12
          • 1970-01-01
          • 2013-10-07
          相关资源
          最近更新 更多