3

I want to remove each copy of object from array:

  const object = [
    { label: "SUA", value: "sua" },
    { label: "SUA", value: "sua" },
    { label: "Florida", value: "florida" }
  ];
  console.log(object.map(i=> [...new Set(i)]))

At the end i have to get like:

const object = [
    { label: "Florida", value: "florida" }
  ];

How to do this in my code?

Sarun UK
  • 6,210
  • 7
  • 23
  • 48
Asking
  • 3,487
  • 11
  • 51
  • 106
  • 3
    Does this answer your question? [Set of objects in javascript](https://stackoverflow.com/questions/5657219/set-of-objects-in-javascript) – Adam Azad Oct 28 '20 at 06:43
  • Do you mean you want an array with unique objects? – Mounir Tanbouzeh El Husseini Oct 28 '20 at 06:43
  • 3
    Does this answer your question? [Remove duplicates from an array of objects in JavaScript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Sarun UK Oct 28 '20 at 06:43
  • What happened to SUA? Why is it removed entirely? – Phil Oct 28 '20 at 06:48
  • 1
    It's really not so clear what you are asking. Are you sure the end result shouldn't be: `[{ label: "SUA", value: "sua" }, { label: "Florida", value: "florida" }] instead of just the florida object? – kaan_a Oct 28 '20 at 06:48
  • You need to have better test cases and problem is not clear what you are trying to solve. [ { label: "SUA2", value: "sua2" }, { label: "SUA", value: "sua" }, { label: "Florida", value: "florida" }, { label: "SUA", value: "sua" }, ] what is your expected result? – Tom Oct 28 '20 at 07:15
  • @Phil I think because there was two of it, and all of them got removed – FZs Oct 28 '20 at 07:29
  • @UKS I don't think so. Appearently, the OP wants to remove all occurrences of duplicates... – FZs Oct 28 '20 at 07:31

5 Answers5

0

You can use 2 for loops to achieve your functionality.

let result = [];
for(let i=0;i<object.length ;i++) {
    let isDuplicate = false;
    for(let j=0; j<object.length;j++) {
        if(object[i].label === object[j].label && object[i].value === object[j].value && i !== j) {
            isDuplicate = true;   
        }
    }
    if(!isDuplicate) result.push(object[i]);
}
Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30
0

Regardless of how many copies of each element you'd like to keep in the array, create a Map by definition Map<Location, number> and tabulate the number of occurrences of each Location object. Afterwards, take each element and append it to an array only once.


type Location = {label: string, value: string};

const object: Location[] = [
    { label: "SUA", value: "sua" },
    { label: "SUA", value: "sua" },
    { label: "Florida", value: "florida" }
];
const objects: Map<Location, number> = new Map();

for (const location of object)
    if (objects.has(location))
        objects.set(location, objects.get(location) + 1);
    else
        objects.set(location, 1);

const filtered: Location[] = [];

for (const location of objects)
    if (location[1] === 1) // You change 1 to any value depending on how many copies of each you'd like.
        filtered.push(location[0];

Note This is TypeScript for clarity, but the concept is the same.

J-Cake
  • 1,526
  • 1
  • 15
  • 35
0

Short way:

const object = [
  { label: "SUA", value: "sua" },
  { label: "SUA", value: "sua" },
  { label: "Florida", value: "florida" }
];

const a = object.filter((v, i, a) => a.findIndex(t => (t.label === 
v.label && t.value === v.value)) === i);

console.log(a);
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29
  • it would fail on object = [ { label: "SUA", value: "sua" }, { label: "SUA", value: "sua" }, { label: "Florida", value: "florida" }, { label: "SUA", value: "sua" }, ]; a = object.filter((v, i, a) => a.findIndex(t => (t.label === v.label && t.value === v.value)) === i); – Tom Oct 28 '20 at 07:13
  • No, you wrong it not fail, you can check it before you wrote a comment @Tom – Baruch Mashasha Oct 28 '20 at 07:16
  • it's wrong because he wanted to remove all the duplicated objs. – Tom Oct 30 '20 at 03:08
0

You can use ES6 set -

const unique = [...new Set(object.map(({value}) => value))].map(e => object.find(({value}) => value == e));

console.log("unique",unique);

//[{label: "SUA", value: "sua"},{label: "Florida", value: "florida"}]

for your problem you can simply use reduce -

const uniq =  object.reduce((a,b)=>{return a.value === b.value ? {} :[b]},[])

console.log(uniq);

//[{label: "Florida", value: "florida"}]
  • object = [ { label: "SUA2", value: "sua2" }, { label: "SUA", value: "sua" }, { label: "Florida", value: "florida" }, { label: "SUA", value: "sua" }, ]; uniq = object.reduce((a,b)=>{return a.value === b.value ? {} :[b]},[]) // {label: "SUA", value: "sua"} ???? how would this work?? – Tom Oct 28 '20 at 07:12
-1

Simple solution with lodash

const object = [
    { label: "SUA", value: "sua" },
    { label: "SUA", value: "sua" },
    { label: "Florida", value: "florida" }
  ];

result = _.xor(_.uniqWith(object,_.isEqual),object)
Tom
  • 81
  • 5