1

How to transform the values of Javascript ES6 Set wrapper into a plain Object? Especially: What is the fastest/shortest way to do so?

I am looking for something like the following that applies to Arrays:

const myArray = Array.from(mySet);

I tried in vain:

// mySet variable is Set containing integers {1, 2, 3, ..., n}

Object.assign({}, mySet);
Object.assign({}, mySet.values());
Object.assign({}, mySet.entries());

Finally, I am thinking about creating a new object, iterating over the set and pushing its entries into the new object. I wonder if there is a more elegant way to do so.

9
  • What do you want as output ? Commented Oct 25, 2017 at 10:29
  • const myObject = {1, 2, 3, ..., n}; Commented Oct 25, 2017 at 10:30
  • 3
    But it's not a valid object Commented Oct 25, 2017 at 10:30
  • Damn. It is true. So I have to stick to array, I suppose. Commented Oct 25, 2017 at 10:31
  • Object.assign( {}, [ ...mySet ]) Commented Oct 25, 2017 at 10:32

2 Answers 2

2

Use ES6 spread syntax:

var mySet = new Set([1, 2, 3]);
var myArray = [ ...mySet];
console.log(myArray);

EDIT: To get an object instead of an array, it should be:

var mySet = new Set([1, 2, 3]);
var obj = {};
[ ...mySet].forEach(el => obj[el] = el);
console.log(obj);

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

4 Comments

So you have basically reiterated the "I know how to convert Set into Array" part of the question?
@pawel The object he want is not valid: { 1, 2, 3, 4 }
Since he asks for an object I'd assume the expected results would be { 1 : 1, 2 : 2, 3 : 3 }
It's ok. I will modify my question slightly. Sorry for confusion.
0

The problem is that Set returns an Interactor and objects don't technically exist without keys. You would need to assign a key for each value and the simplest way of doing that is to spread it into a new array, so that the index of the array becomes the key.

// returns { "0": 1, "1": 2, "2": 3, ..., "i": n }
{...{}, ...[...mySet]};

// Same as above.
Object.assign({}, Array.from(mySet));

If however, you want the key and value to be the same in the object, you can run a reducer method on the parsed array, with the initial value as the returned object.

const myReducer = (acc, cur) => {
  // You don't need to stringify, if you can ensure it's always numbers.
  acc[JSON.stringify(cur)] = cur;
  return acc;
};

// Returns { "1": 1, "2": 2, "3": 3, ..., "n": n }
Array.from(mySet).reduce(myReducer, {});

// One liner, as per above, just less readable.
[...mySet].reduce((acc, cur) => !(acc[JSON.stringiy(cur)] = cur) || acc, {});

Disclaimer: As Set can have any primitive value, at any location, the keys can get a bit crazy when assigning objects to it.

// Returns { "1": 1, "{\"test1\":1}": { "test1": 1 } }
[...new Set([1, {test1:1}])].reduce(myReducer, {});

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.