26

i have an array of objects like this:

arr = [
    {label: Alex, value: Ninja},
    {label: Bill, value: Op},
    {label: Cill, value: iopop}
]

This array is composed when my react component is rendered. The i user Array.prototype.unshift for adding a desired element in the top of my array. So i write arr.unshift({label: All, value: All}). When my component first rendered my array is successfully created as i desire. But when i rerender it it shows me the array with the value {label: All, value: All} as duplicate. To be more specific it is shown something like this:

arr = [
    {label: All, value: All},
    {label: All, value: All},
    {label: Alex, value: Ninja},
    {label: Bill, value: Op},
    {label: Cill, value: iopop}
]

How can i fix this? I tried the methods described in a specific topic here but it didn't work

Mukesh Soni
  • 6,646
  • 3
  • 30
  • 37
RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • I've read this it is similar but not exactly duplicate. My case is different – RamAlx Aug 01 '17 at 14:02
  • 3
    Yeah, sure, they always are ... Please add the code you've tried. – Teemu Aug 01 '17 at 14:02
  • What should that be strings or identifiers? – Jonas Wilms Aug 01 '17 at 14:05
  • Yes, they are strings just wanted to show the case i'm facing – RamAlx Aug 01 '17 at 14:05
  • 1
    How is your case different? – Mukesh Soni Aug 01 '17 at 14:07
  • Please add some code of your component ? – Arun Redhu Aug 01 '17 at 14:09
  • const euroTrade = cashAmounts.cash_amounts.find(avCash => (avCash.currency === 'EUR')); let euroWithdrawAv; if (typeof euroTrade === 'undefined') { euroWithdrawAv = 0; } else { euroWithdrawAv = euroTrade.withDrawAmount; } cashTypes.unshift({ label: intl.formatMessage({ id: 'all.description' }), value: 'ALL' }); let submitCashSearch; if (currentCashRadio === 'cashTransaction') { submitCashSearch = submitCashTransactionsSearch; } else { submitCashSearch = submitCashTransfersSearch; } – RamAlx Aug 01 '17 at 14:10
  • simply add `if (! arr.find(e => e.label === 'All' && e.value === 'All'))` before `unshift` – baao Aug 01 '17 at 14:11

6 Answers6

97

You can use array#reduce and array#some.

const arr = [
    {label: 'All', value: 'All'},
    {label: 'All', value: 'All'},
    {label: 'Alex', value: 'Ninja'},
    {label: 'Bill', value: 'Op'},
    {label: 'Cill', value: 'iopop'}
]

var result = arr.reduce((unique, o) => {
    if(!unique.some(obj => obj.label === o.label && obj.value === o.value)) {
      unique.push(o);
    }
    return unique;
},[]);
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • Want to give 1k upvote, but you have to manage in 1 :) . Well thanks for this perfect answer – kartik tyagi Aug 31 '21 at 06:30
  • how do yo do this if value (instead of a string value ) is equal to some array of strings ? and I need to remove the repeated strings in there and return arr ? – Gel Nov 03 '22 at 13:38
24

One liner solutions:

Unique by label and value

arr.filter((v,i,a)=>a.findIndex(v2=>(v.label === v2.label && v.value===v2.value))===i)

Unique by all properties of the object:

arr.filter((v,i,a)=>a.findIndex(v2=>(JSON.stringify(v) === JSON.stringify(v2)))===i)
chickens
  • 19,976
  • 6
  • 58
  • 55
10

This code worked for me,

const addresses = [...]; // Some array I got from async call

const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
 .map(id => {
   return addresses.find(a => a.id === id)
 })
Anusha Bhat
  • 391
  • 4
  • 6
1
const things = {
  thing: [
    { id: '12345', name: 'First name' },
    { id: '12345', name: 'Second name' },
    { id: '34536', name: 'Third name' }, 
  ],
};

const RemoveDuplicates = (array, key) => {
  return array.reduce((arr, item) => {
    const removed = arr.filter(i => i[key] !== item[key]);
    return [...removed, item];
  }, []);
};

console.log(RemoveDuplicates(things.thing, 'id'));
Azhar
  • 20,500
  • 38
  • 146
  • 211
1
function removeDuplicates(array, key) {
   let lookup = {};
   array.forEach(element => {
     lookup[element[key]] = element
   });
   return Object.keys(lookup).map(key => lookup[key]);
};

removeDuplicates(array,'objectKey');
David Buck
  • 3,752
  • 35
  • 31
  • 35
Ali Akbar
  • 31
  • 3
-2

ES1 compatible :

const a = [
    {label: 'All', value: 'All'},
    {label: 'All', value: 'All'},
    {label: 'Alex', value: 'Ninja'},
    {label: 'Alex', value: 'Ninja'},
    {label: 'Alex', value: 'Ninja'}
]

for (let i = 0; i < a.length; ++i)
    for (let j = 0; j < a.length; ++j)
        if (i !== j && a[i].label === a[j].label && a[i].value === a[j].value)
            a.splice(j, 1);            
console.log(a);
Locoder
  • 22
  • 1
  • 4