1

First of, I am aware that there are LOTS of answers on SO on this, but I am having some issues with them, that is why I post another question on this topic.

So here is my Array of Objects:

0: {id: 'lAYOUT', label: 'lAYOUT', items: 'val1'}
1: {id: 'tecst', label: 'tecst', items: 'val1'}
2: {id: 'tecst', label: 'tecst', items: 'val1'}

I am trying to filter out that there would be only 2 values, since there are 2 objects in array that are the same. I would like to make unique objects by items and label.

This is how I am trying to do it with lodash:

const filteredArray = uniq(nestedItems, (item, key, a) => item.items && item.label)

But it keeps returning me all 3 elements still.

I also tried it like this:

const filteredArray = [...new Set(nestedItems)]
Nightcorey
  • 25
  • 5
  • 1
    What is the `key` in array ?? – Mayur Vaghasiya Jan 11 '22 at 09:52
  • 1
    Does this answer your question? [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Nitheesh Jan 11 '22 at 09:58
  • 1
    Does this answer your question? [How to remove all duplicates from an array of objects?](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) – Mayur Vaghasiya Jan 11 '22 at 10:00
  • @MayurVaghasiya My bad, I copied/paste wrong. Instead of `key` it should be `label`, but issue exist, I just copied it on SO wrongly. – Nightcorey Jan 11 '22 at 10:04

3 Answers3

1

Using Filter get the particular object value, index and array. Using FindIndex get the particular array object. and compare filter object and findindex object, if it return false then push in new array! and make new unique array !

Try this code !

 let arr = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' },
    { id: 'tecst', label: 'tecst', items: 'val1' },
    { id: 'tecst', label: 'tecst', items: 'val1' }];

    let newArr = arr.filter((value, index, self) =>
      index === self.findIndex((t) => (
        t.label === value.label && t.items === value.items
      ))
    );
    console.log(newArr, 'newArr');
Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24
1

You can use hash grouping to filter by several keys:

const data = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }];

const unique = Object.values(data.reduce((acc, obj) => {
  const hash = `${obj.id}-${obj.label}`;
  acc[hash] = obj;
  return acc;
}, {}));

console.log(unique);
.as-console-wrapper{min-height: 100%!important; top: 0}

Same result with lodash

const data = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }];

const result = _.uniqWith(data, (o1, o2) => `${o1.id}-${o1.label}` === `${o2.id}-${o2.label}`);

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js" integrity="sha512-2iwCHjuj+PmdCyvb88rMOch0UcKQxVHi/gsAml1fN3eg82IDaO/cdzzeXX4iF2VzIIes7pODE1/G0ts3QBwslA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

const data = [
  { id: 'id1', label: 'label1', items: 'items1' }, 
  { id: 'id2', label: 'label2', items: 'items2' },
  { id: 'id1', label: 'label1', items: 'items2' }
];

const unique = (...keys) => [
    ...new Map(data.map(item => [keys.map(key => item[key]).join(), item])).values()
];

console.log(1, unique('label'));
console.log(2, unique('label','items'));
Ian
  • 1,198
  • 1
  • 5
  • 15
  • Hey @Ian the problem with this solution in my case is that I need to make it unique by 2 keys: `items` AND `label`. WIth this unique function I can only make it unique only by 1 property. – Nightcorey Jan 11 '22 at 10:06
  • Hi @Nightcorey I misunderstood the meaning of your question, thinking that only one key is needed to distinguish, I modified my answer, now you can use multiple keys – Ian Jan 12 '22 at 08:11