代码之家  ›  专栏  ›  技术社区  ›  Lemon Kazi

json数据检查同一id是否重复

  •  0
  • Lemon Kazi  · 技术社区  · 6 年前

    我有一个json数据,其中一个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,但是在那里我发现修改了数据结构,所以我不确定我是否会得到我想要的输出。

    4 回复  |  直到 6 年前
        1
  •  4
  •   Petter Friberg Onceler    6 年前

    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. 然后过滤数组,每当我们找到一个不在映射中的条目时,我们就把它添加到列表中并返回true。如果我们已经在列表中有了它,我们将返回false以丢弃该条目。

        2
  •  1
  •   brk    6 年前

    你可以用 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
  •  1
  •   Orelsanpls    6 年前

    使用 Array.reduce Array.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

        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
  •  0
  •   Thijs    6 年前

    你可以使用 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));