Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
Why is Set() repeating values?

I´m trying to use Set() in js to retrieve some 'unique data', but it keeps returning repeated values to me.

Console debug: Console debug

Code:

userStories.add({
   user_id: storie.user_id,
   avatar: storie.user.avatar,
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Sounds like you are trying to avoid duplicating users?

It is clever to try to use a set, because sets do avoid duplication, but unfortunately they only work in the way you intended if the element is a primitive data type such as String or Number, and not if it is an object, i.e. of the form { key: value, ... }.

If the element is just a string, e.g. "Bob", because two strings "Bob" and "Bob" are considered identical by the Set, and so will be entered as a single element.

However, if your element is an object, e.g. {name: "Bob"}, then two elements defined separately as {name: "Bob"} and {name: "Bob"} will be considered unequal, so will be entered as two different elements in the Set.

Try using a simple Object

Initialise like this:

userStories = {}

And then for each entry:

userStories[user_id]=({
   avatar: storie.user.avatar,
});

This way, you end up with this structure:

userStories: {
    1234: { avatar: "abc.jpg" },
    1258: { avatar: "def.jpg" },
    etc
}

You can use this Object structure in much the way you were using the Set, with two advantages:

(a) If you put in two identical entries such as 1234: { avatar: "abc.jpg" }, only one will be stored.

(b) You can directly access the avatar of any one person, by their id. (With the Set, you would still have to somehow search the elements to find the right one.)

about 4 years ago · Juan Pablo Isaza Report

0

The Set object lets you store unique values of any type, whether primitive values or object references not just objects. See reference MDN

const mySet = new Set();
const object1 = {a:1, b:1};
mySet.add(object1);
mySet.add(object1);
console.log(mySet); // The set will be having only 1 entry as both the times we added the same reference of object1.

However if you do try to insert different references of object the set keeps on adding the objects.For example:

const mySet = new Set();
const object1 = {a:1, b:1};
mySet.add(object1);
mySet.add({a:1,b:1}); // This is a new object not the object1
console.log(mySet); It will have 2 entries

Not only objects same goes with Arrays if you try to do like above.

about 4 years ago · Juan Pablo Isaza Report

0

Objects in JavaScript are reference types, which means that when you create a new object in your function, JavaScript thinks they are different (even when they have the same values).

From the article:

If they’re distinct objects, even if they contain identical properties, the comparison will result in false.

var arr1 = ['Hi!'];
var arr2 = ['Hi!'];
console.log(arr1 === arr2); // -> false
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!