Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove duplicate values from json data [duplicate]

Tags:

json

jquery

I have a json data like this:

{
  "_id": ObjectId("5306e7fd80b1027ad2653db4"),
  "testada": "ecom",
  "id": "27" 

} {
  "_id": ObjectId("5306e81880b1027ad2653db5"),
  "testada": "alorta",
  "id": "27" 
} {
  "_id": ObjectId("5306e81880b1027ad2653db6"),
  "testada": "france24",
  "id": "23" 
}
{
  "_id": ObjectId("5306e81880b1027ad2653db6"),
  "testada": "seloger",
  "id": "23" 
}

From this I have to remove one duplicate entry with id:27, the same as case id:23 my resulting json looks as follows:

{
  "_id": ObjectId("5306e81880b1027ad2653db5"),
  "testada": "alorta",
  "id": "27" 
} {
  "_id": ObjectId("5306e81880b1027ad2653db6"),
  "testada": "france24",
  "id": "23" 
}

how it is possible

like image 782
Sush Avatar asked Mar 30 '26 03:03

Sush


1 Answers

Fiddle Demo

var arr = [], //to collect id values 
    collection = []; //collect unique object

$.each(json_all, function (index, value) {
    if ($.inArray(value.id, arr) == -1) { //check if id value not exits than add it
        arr.push(value.id);//push id value in arr
        collection.push(value); //put object in collection to access it's all values
    }
});
console.log(collection); //you can access it values like collection[index].keyName
console.log(arr);
like image 54
Tushar Gupta - curioustushar Avatar answered Apr 02 '26 02:04

Tushar Gupta - curioustushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!