JavaScript Array to Set

后端 未结 4 1357
时光说笑
时光说笑 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:37

    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.

提交回复
热议问题