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?

8
  • 3
    Does this answer your question? Set of objects in javascript Commented Oct 28, 2020 at 6:43
  • Do you mean you want an array with unique objects? Commented Oct 28, 2020 at 6:43
  • 3
    Does this answer your question? Remove duplicates from an array of objects in JavaScript Commented Oct 28, 2020 at 6:43
  • What happened to SUA? Why is it removed entirely? Commented Oct 28, 2020 at 6: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? Commented Oct 28, 2020 at 6:48

5 Answers 5

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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"}]

1 Comment

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

3 Comments

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);
No, you wrong it not fail, you can check it before you wrote a comment @Tom
it's wrong because he wanted to remove all the duplicated objs.
-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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.