jee*_*taz 8 javascript ecmascript-6
请看下面的脚本.我正在使用Chrome进行测试.
/*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]}
Run Code Online (Sandbox Code Playgroud)
items.has([5,6,7,8])?items.add([5,6,7,8])?Tus*_*har 12
为什么它会返回false items.has([5,6,7,8])?
来自MDN
Set对象允许您存储任何类型的唯一值,无论是原始值还是对象引用.
使用引用而不是值来比较对象.集使用SameValueZero(x, y)比较算法来比较值.如果x和y是相同的Object值,则返回Return .否则,返回.truefalse
为什么允许重复值?我认为"一个集合在一个有序的值列表中,不能包含重复"
与#1相同.如果已经在集合中添加了相同的对象(不仅仅是相同的外观),则称非原始值已经存在于集合中.
如何访问添加的数组items.add([5,6,7,8])?
您要创建变量并将变量添加到集合中.然后,此变量可用于检查set是否具有该数组.
| 归档时间: |
|
| 查看次数: |
5813 次 |
| 最近记录: |