1
var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}


console.log(Object.keys(noDuplicates));   // []
console.log(Object.values(noDuplicates)); // []

I basically want to access the 'e', 'y', and the 's' of the set called 'noDuplicates'.

var setToArray = [];
for (spot in noDuplicates) {
    setToArray.push(Object.keys(noDuplicates)[spot])
}
BayCode
  • 93
  • 11
  • 2
    Did you check the documentation for [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)? –  Jan 02 '19 at 22:22
  • 1
    Every objects has "keys". A `Set` is a special object that stores its values internally. Use its API to access the values. – Felix Kling Jan 02 '19 at 22:25

2 Answers2

2

You can use array spread syntax to convert a Set to array:

var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}
var setToArray = [...noDuplicates];

console.log(setToArray);

You can also use Set.forEach() or a for...of loop to access the Set's values directly:

var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}

noDuplicates.forEach(v => console.log(v));

for(const v of noDuplicates) {
  console.log(v);
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • This is the perfect answer. Thanks! – BayCode Jan 02 '19 at 22:36
  • noDuplicates.forEach(v => console.log(v)); If I understand this correctly, you created a null function named v that prints out the e, y, and s once each where v acts as the iterator/index – BayCode Jan 02 '19 at 22:40
0

noDuplicates is a Set which provides an iterator. Simply use for...of or the spread operator [... noDuplicates] instead of for...in. Better yet, convert your set into an array directly with Array.from:

let setToArray = Array.from(new Set([1, 1, 1, 2, 2]));
console.log(setToArray);
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 2
    [`...` is not an operator](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508) – Felix Kling Jan 02 '19 at 22:25
  • 2
    @FelixKling The spread syntax is commonly known as the "spread operator" within the community. Usages of this term is ubiquitous: [Ref 1](https://redux.js.org/recipes/using-object-spread-operator), [Ref 2](https://eslint.org/docs/rules/prefer-spread), [Ref 3](https://www.geeksforgeeks.org/javascript-spread-operator/). – Derek 朕會功夫 Jan 02 '19 at 22:28
  • @FelixKling Too late for that debate. – ibrahim mahrir Jan 02 '19 at 22:30
  • 1
    I would say, let's at least try here at SO to instate good naming habits. – trincot Jan 02 '19 at 22:32
  • I believe that people who know better have the obligation to use the proper terminology, to not spread misconceptions further (no pun intended). All these references could be updated/corrected (unless previous such attempts have been shut down). In fact, the MDN documentation on this was updated at some point. – Felix Kling Jan 02 '19 at 22:40