forof循环】的更多相关文章

let set = new Set(); //set方法去除重复的数据 [1, 2, 3, 4, 2, 8, 4].map(function (elem) { set.add(elem); //遍历完用add添加至set数组中 }) for (let elem of set) { //利用for...of循环遍历出来 console.log(elem) } for-of循环不仅支持数组,还支持大多数类数组对象 for-of循环也支持字符串遍历,它将字符串视为一系列的Unicode字符来进行遍历…
作者 Jason Orendorff  github主页  https://github.com/jorendorff 我们如何遍历数组中的元素?20年前JavaScript刚萌生时,你可能这样实现数组遍历: for (var index = 0; index < myArray.length; index++) { console.log(myArray[index]); } 自ES5正式发布后,你可以使用内建的forEach方法来遍历数组: myArray.forEach(function…
遍历数组要怎么做,可能你首先想到的会是for循环,当然for循环在JavaScript 刚萌生的时候就出现了,想到它也是理所当然的 var a=[[1,2],[3,4],5] for(var i=0;i<a.length;i++){ console.log(a[i]); } for循环略显臃肿, 在ES5中有了forEach来遍历数组,似乎变得简洁了许多 a.forEach(function (value) { console.log(value); }) but,forEach不能使用 bre…
SE5之前我们可以用for循环来遍历数组,SE5为数组引进了新的方法forEach(),方便了很多,但是该方法不能够通过break或者return返回外层函数. arr.forEach(function(value){ console.log(value); }) ES6定义了一个更好的遍历数组的方法for-of循环,该方法的强大在于可以遍历任何具有迭代器的对象,例如数组.NodeList对象.Map和Set对象.还可用于遍历字符串,将其视为一系列的Unicode字符来遍历,即中文也算一个字符.…
forEach不支持break for-in把数组当做对象来遍历,但是只能遍历出索引值 for-of循环可以遍历出数组的每一项值,支持break 1.for-in示范: 2.for-of示范 3.for-of支持break…
我们如何遍历一个数组呢?在20年前,我们是这样遍历一个数组的: var myArr = []; for (var i = 0; i < arr.length; i++) { console.log(myArr[i]) } 自从es5正式发布以后,我们可以用内置的forEach方法遍历一个数组: myArr.forEach(function(e, index){ console.log(e); console.log(index); }) 这段代码看起来更加简洁,但这种方法也有一个小缺陷:你不能使…
一.语法 1.遍历数组 let myArr=[1,2,3,4,5]; for (let ele of myArr) { console.log(ele); } let myArr=[1,2,3,4,5]; for (const ele of myArr) { console.log(ele); } 2.遍历字符串 let myStr=“camille”; for (let ele of myStr) { console.log(ele); } 3.循环一个拥有enumerable属性的对象 fo…
Iterator 遍历器的作用:为各种数据结构,提供一个同意的,简便的访问接口.是的数据结构的成员能够按某种次序排列.ES6 新增了遍历命令 for...of 循环,Iterator接口主要供 for...of 消费. 1.手写Iterator接口. const arr=[ 1,2,3 ]; function iterator(arr){ let index=0; return { return index<arr.length?{value:arr[index++],done:false}:{…
以数组为例,JavaScript 提供多种遍历语法.最原始的写法就是for循环. for (var index = 0; index < myArray.length; index++) { console.log(myArray[index]); } 这种写法比较麻烦,因此ES5中数组提供内置的forEach方法. myArray.forEach(function (value) { console.log(value); }); 这种写法的问题在于,无法中途跳出forEach循环,break…
/* 1. 遍历数组 2. 遍历Set 3. 遍历Map 4. 遍历字符串 5. 遍历伪数组 6. 可迭代的对象 */var arr = [2,3,4];for(let ele of arr) { console.log(ele);} var set = new Set([3, 5, 7, 5]);for(let item of set) { console.log(item);} var map = new Map([["name", 'www'], ["age"…