Skip to content Skip to sidebar Skip to footer

Json Data Check If Same Id Repeat

I have a json data there one id has different color_id. So from there I just want to check if same id repeat then just keep first one Here is my sample JSON var data= [{ 'id': '1',

Solution 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 )

Explaination:

  1. You create a map where we store ids.
  2. Then filter the array and everytime we find an entry that is not in the map we add it to the list and return true. If we already have it in the list we return false to discard that entry.

the last part of the conditional is using the fact that an assignment returns the assigned value.

Solution 2:

You can use reduce to create a new array and in this new array use findIndex to check if this new array has an object with same id. If there exist an object with same id then dont push another object with same 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)

Solution 3:

Using Array.reduce and 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);

Or Array.filter as it was a good idea from @JGoodgive, but kinda different

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);

Solution 4:

You can use a Set to keep track of the IDs already processed.

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" }];
  
functiondedupe(items) {
  // Create a set to keep track of IDs already encountered.const
    idSet = newSet();
  // 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)) {
      returnfalse;
    }
    
    // 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.returntrue;
  });
}    

console.log(dedupe(data));

Post a Comment for "Json Data Check If Same Id Repeat"