Answers for "js remove json value duplicates"

1

js remove json value duplicates

var data = [{
    "name": "Peter",
    "age": 30,
    "hair color": "brown"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}]

 data = this.data.filter((obj, pos, arr) => {
   return arr
     .map(mapObj => mapObj.name)
     .indexOf(obj.name) == pos;
 });

console.log(data);
/*
[{
    "name": "Peter",
    "age": 30,
    "hair color": "brown"
}, {
    "name": "Steve",
    "age": 55,
    "hair color": "blonde"
}]
*/
Posted by: Guest on April-29-2021
0

js remove json value duplicates

var json = [
    {"text":"menu1","parent":"#","id":"128102"},
    {"text":"menu1.1","parent":"128102","id":"128103"},
    {"text":"menu1.1","parent":"128102","id":"128103"}
];

var ids = [];
var clean = [];

$.each(json, function(index, value) {
    if($.inArray(value.id, ids) == -1)
    {
        ids.push(value.id);
        clean.push(value);
    }
});
Posted by: Guest on July-15-2020

Code answers related to "js remove json value duplicates"

Code answers related to "Javascript"

Browse Popular Code Answers by Language