4

If there are two JSON objects in an array with same value for a particular field, then I want to mark them as duplicate. I want to remove one of them. Similarly, when there are multiple duplicate, I only want to keep the last object(latest).If this is input:

names_array = [
    {name: "a",  age: 15},
    {name: "a",  age: 16},
    {name: "a",  age: 17},
    {name: "b",  age: 18}
    {name: "b",  age: 19}];

I want the output to be

names_array_new = 
    {name: "a",  age: 17},
    {name: "b",  age: 19}];

I have searched for this but only found how to remove duplicates when entire objects are same.

SS306
  • 157
  • 1
  • 3
  • 9
  • 1
    Check this: [How to Remove Duplicate objects from JSON Array?](http://stackoverflow.com/questions/23507853/remove-duplicate-objects-from-json-array) it may be helpful. – Umair M Aug 12 '15 at 10:31
  • Thank you. This was also helpful. – SS306 Aug 12 '15 at 10:53

4 Answers4

5

This should do it:

names_array = [
    {name: "a",  age: 15},
    {name: "a",  age: 16},
    {name: "a",  age: 17},
    {name: "b",  age: 18},
    {name: "b",  age: 19}];

function hash(o){
    return o.name;
}    

var hashesFound = {};

names_array.forEach(function(o){
    hashesFound[hash(o)] = o;
})

var results = Object.keys(hashesFound).map(function(k){
    return hashesFound[k];
})

The hash function decides which objects are duplicates, the hashesFound object stores each hash value together with the latest object that produced that hash, and the results array contains the matching objects.

codebox
  • 19,927
  • 9
  • 63
  • 81
1

A slightly different approach:

var names_array = [
    { name: "a", age: 15 },
    { name: "a", age: 16 },
    { name: "a", age: 17 },
    { name: "b", age: 18 },
    { name: "b", age: 19 }
];

var names_array_new = names_array.reduceRight(function (r, a) {
    r.some(function (b) { return a.name === b.name; }) || r.push(a);
    return r;
}, []);

document.getElementById('out').innerHTML = JSON.stringify(names_array_new, 0, 4);
<pre id="out"></pre>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
var names_array = [
{name: "a", age: 15},
{name: "a", age: 16},
{name: "a", age: 17},
{name: "b", age: 18},
{name: "b", age: 19}];

function removeDuplicate(arr, prop) {
var new_arr = [];
var lookup = {};
for (var i in arr) {
    lookup[arr[i][prop]] = arr[i];
}
for (i in lookup) {
    new_arr.push(lookup[i]);
}
return new_arr;}
var newArray = removeDuplicate(names_array, 'name');
console.log("Result "+newArray);
kiran Sp
  • 153
  • 7
0

Array.from(new Set(brand.map(obj => JSON.stringify(obj)))).map(item => JSON.parse(item))

  • 1
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 24 '18 at 05:01