29

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

8
  • I've read this it is similar but not exactly duplicate. My case is different Commented Aug 1, 2017 at 14:02
  • 3
    Yeah, sure, they always are ... Please add the code you've tried. Commented Aug 1, 2017 at 14:02
  • What should that be strings or identifiers? Commented Aug 1, 2017 at 14:05
  • Yes, they are strings just wanted to show the case i'm facing Commented Aug 1, 2017 at 14:05
  • 1
    How is your case different? Commented Aug 1, 2017 at 14:07

6 Answers 6

107

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Want to give 1k upvote, but you have to manage in 1 :) . Well thanks for this perfect answer
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 ?
25

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)

2 Comments

v,i,a == value, index, array
It doesn't work if the keys are not in the same order.
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)
 })

1 Comment

This works for 1 property (id) but not 2 as the question asked.
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'));

Comments

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');

Comments

-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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.