Saturday 22 December 2018

JavaScript: How to convert an array to set?

Set constructor takes an iterable as argument and populate the elements of iterator.

Syntax
new Set(iterable)

HelloWorld.js

var countries =  new Array();

countries.push("India");
countries.push("Australia");
countries.push("Canada");
countries.push("Germany");

var countriesSet = new Set(countries)

console.log(countries);
console.log(countriesSet);

Output
Array(4) [ "India", "Australia", "Canada", "Germany" ]
Set(4) [ "India", "Australia", "Canada", "Germany" ]


Previous                                                 Next                                                 Home

No comments:

Post a Comment