JavaScript Array to Set

后端 未结 4 1349
时光说笑
时光说笑 2020-12-23 23:46

MSDN 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()

4条回答
  •  执念已碎
    2020-12-24 00:26

    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

提交回复
热议问题