0

I find the following answer help me a lot in removing duplicate object array which contains duplicates.

I've made a fork of the example which I modified.

The function related:

const uniqueArray = things.thing.filter((thing,index) => {
  return index === things.thing.findIndex(obj => {
    return JSON.stringify(obj) === JSON.stringify(thing);
  });
});

For example I have:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
]

It would return:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
]

How to remove the repeating place name which contains the same name?

Expected output:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"}
]
Shinjo
  • 677
  • 6
  • 22
  • 1
    You may want to use Array.reduce instead, where the accumulator is the "filtered" object, and the next item is pushed only if the accumulator doesn't contain the desired item. `const uniqueArray = things.thing.reduce((acc, next) => { if (acc.find(i => i.place === next.place)) return acc; else return (acc.push(next), acc); }, []); ` – briosheje Apr 09 '19 at 10:41
  • Sorry, I forgot to put the expected output. – Shinjo Apr 09 '19 at 10:48

3 Answers3

2

Check this

  const things = [
        {"place":"here","name":"stuff"},
        {"place":"there","name":"morestuff"},
        {"place":"there","name":"morestuff"},
        {"place":"herehere","name":"stuff"}
    ]

    const uniqueArray = things.reduce((accumulator, currentValue) => {
        if (accumulator.find(a => a.name === currentValue.name))
            return accumulator;
        else
            return (accumulator.push(currentValue), accumulator);
    }, []);

Output

    [ { place: 'here', name: 'stuff' },
      { place: 'there', name: 'morestuff' } ]
Dinesh
  • 334
  • 1
  • 5
  • 14
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce check this you can understand how it works – Dinesh Apr 09 '19 at 10:58
2

You can reduce over the array of objects. Simply, if an object with a key value the same as the current object already exists in the accumulator, don't add it again.

Here's a function that allows you specify which key you want to dedupe:

const arr = [
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
];

// Accepts an array and a key that should have the
// duplicates removed
function remove(arr, key) {

  // Iterate over the array passing in the accumulator
  // and the current element
  return arr.reduce((acc, c) => {

    // If there is an object in the accumulator with the
    // same key value as the current element simply return the
    // accumulator
    if (acc.find(obj => obj[key] === c[key])) return acc;

    // Otherwise add the current element to the accumulator
    // and return it
    return acc.concat(c);
  }, []);
}

function showJSON(arr, id) {
  const json = JSON.stringify(arr, null, 2);
  document.querySelector(`#${id} code`).textContent = json;
}

// remove duplicate places
showJSON(remove(arr, 'place'), 'places');

// remove duplicate names
showJSON(remove(arr, 'name'), 'names');
<div id="places">
Removed duplicate places
<pre><code></code></pre>
</div>

<div id="names">
Removed duplicate names
<pre><code></code></pre>
</div>
Andy
  • 61,948
  • 13
  • 68
  • 95
0

You can use array reduce with filter

let data=[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
]

// Using reduce() to separate the contents we want
let result=data.reduce((acc,value)=>{
  if(acc.filter(val=>val.name==value.name).length==0) // checking the accumulator if it already containsa the value
  {
    acc.push(value); // if the array returned is of length==0 we can push in it
  }
return acc;

},[])
console.log(result);

See Array Filter, Array.prototype.Reduce

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46