当前位置:网站首页>ES6 Beginner to Mastery #13: Extension Methods for Arrays 2

ES6 Beginner to Mastery #13: Extension Methods for Arrays 2

2022-08-10 00:56:00 Kay Xiaomo

说明

ES6 从入门到精通系列(全23讲)学习笔记.

遍历器

entries(),keys(),values(), 返回一个遍历器,可以使用 for···of 循环进行遍历

  • keys():The values traversal
  • values():对键值对遍历
  • entries():对键名遍历
console.log(["k", "a", "i", "m", "o"].keys());
console.log(["k", "a", "i", "m", "o"].values());
console.log(["k", "a", "i", "m", "o"].entries());

在这里插入图片描述

for (let key of ["k", "a", "i", "m", "o"].keys()) {
    
	console.log(key);
}
for (let value of ["k", "a", "i", "m", "o"].values()) {
    
	console.log(value);
}
for (let [key, value] of ["k", "a", "i", "m", "o"].entries()) {
    
	console.log(key, value);
}

在这里插入图片描述

let kaimo = ["k", "a", "i", "m", "o"].entries();
console.log(kaimo.next().value)
console.log(kaimo.next().value)
console.log(kaimo.next().value)
console.log(kaimo.next().value)
console.log(kaimo.next().value)
console.log(kaimo.next().value)

在这里插入图片描述

includes

includes() 返回一个布尔值,表示某个数组是否包含给定的值

console.log([1,2,3].indexOf(1) > -1)
console.log([1,2,3].includes(1))

在这里插入图片描述

原网站

版权声明
本文为[Kay Xiaomo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092236165593.html