/*declare a new set*/
var items = new Set()
/*add an array by declaring as array type*/
var arr = [1,2,3,4];
items.add(arr);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4]}
/*add an array directly as argument*/
items.add([5,6,7,8]);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8]}
/*print type of items stored in Set*/
for (let item of items) console.log(typeof item); //object, object
/*check if item has array we declared as array type*/
console.log(items.has(arr)); // true
/*Now, check if item has array we added through arguments*/
console.log(items.has([5,6,7,8])); //false
/*Now, add same array again via argument*/
items.add([1,2,3,4]);
/*Set has duplicate items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4]}
- 为什么在
items.has([5,6,7,8])
处返回false? - 为什么允许重复的值?我认为“集合是一个有序的值列表,不能包含重复项”
- 如何访问由
items.add([5,6,7,8])
添加的数组?
[5, 6, 7, 8] !== [5, 6, 7, 8]
这段代码也返回 false,每个数组都是一个独立的对象。 - 4esn0ktrue
。 - xyres