Thomas
Thomas

Reputation: 6690

JavaScript Array to Set

MDN references JavaScript's Set collection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name:

var array = [
    {name: "malcom", dogType: "four-legged"},
    {name: "peabody", dogType: "three-legged"},
    {name: "pablo", dogType: "two-legged"}
];

How do I convert this array to a set? More specifically, is it possible to do this without iterating over the above array? The documentation is relatively lacking (sufficient for instantiated sets; not for conversions - if possible).

I may also be thinking of the conversion to a Map, for removal by key. What I am trying to accomplish is an iterable collection that can be accessed or modified via accessing the elements primarily via a key (as opposed to index).

Conversion from an array to the other being the ultimate goal.

Upvotes: 337

Views: 497967

Answers (6)

Babatunde Mustapha
Babatunde Mustapha

Reputation: 2663

const categoryWithDuplicates =[
  'breakfast', 'lunch',
  'shakes',    'breakfast',
  'lunch',     'shakes',
  'breakfast', 'lunch',
  'shakes'
]

const categoryWithoutDuplicates =[...new Set(categoryWithDuplicates)];
console.log(categoryWithoutDuplicates)

Output: [ 'breakfast', 'lunch', 'shakes' ]

Upvotes: 14

lizardkingLK
lizardkingLK

Reputation: 41

var yourSetAsArray = Array.from(new Set([1, 2, 2, 3, 3, 4, 4])); 
// returns [1, 2, 3, 4]

Upvotes: -1

levi
levi

Reputation: 25141

Just pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter.

var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));

See here

Upvotes: 487

Ashlesh
Ashlesh

Reputation: 357

By definition "A Set is a collection of values, where each value may occur only once." So, if your array has repeated values then only one value among the repeated values will be added to your Set.

var arr = [1, 2, 3];
var set = new Set(arr);
console.log(set); // {1,2,3}


var arr = [1, 2, 1];
var set = new Set(arr);
console.log(set); // {1,2}

So, do not convert to set if you have repeated values in your array.

Upvotes: 13

Ryan Shillington
Ryan Shillington

Reputation: 25187

If you start out with:

let array = [
    {name: "malcom", dogType: "four-legged"},
    {name: "peabody", dogType: "three-legged"},
    {name: "pablo", dogType: "two-legged"}
];

And you want a set of, say, names, you would do:

let namesSet = new Set(array.map(item => item.name));

Upvotes: 65

AstroCB
AstroCB

Reputation: 12367

What levi said about passing it into the constructor is correct, but you could also use an object.

I think what Veverke is trying to say is that you could easily use the delete keyword on an object to achieve the same effect.

I think you're confused by the terminology; properties are components of the object that you can use as named indices (if you want to think of it that way).

Try something like this:

var obj = {
    "bob": "dole",
    "mr.": "peabody",
    "darkwing": "duck"
};

Then, you could just do this:

delete obj["bob"];

The structure of the object would then be this:

{
    "mr.": "peabody",
    "darkwing": "duck"
}

Which has the same effect.

Upvotes: 3

Related Questions