js中forEach,for in,for of循环的用法
from:https://www.cnblogs.com/amujoe/p/8875053.html
一、一般的遍历数组的方法:
var array = [1,2,3,4,5,6,7];
for (var i = 0; i < array.length; i) {
console.log(i,array[i]);
}
二、用for in的方遍历数组
for(let index in array) {
console.log(index,array[index]);
};
三、forEach

array.forEach(v=>{
console.log(v);
});
array.forEach(function(v){
console.log(v);
});

四、用for in不仅可以对数组,也可以对enumerable对象操作
var A = {a:1,b:2,c:3,d:"hello world"};
for(let k in A) {
console.log(k,A[k]);
}
五、在ES6中,增加了一个for of循环,使用起来很简单

for(let v of array) {
console.log(v);
};
let s = "helloabc";
for(let c of s) {
console.log(c);
}

总结来说:for in总是得到对像的key或数组,字符串的下标,而for of和forEach一样,是直接得到值
结果for of不能对象用
对于新出来的Map,Set上面

var set = new Set();
set.add("a").add("b").add("d").add("c");
var map = new Map();
map.set("a",1).set("b",2).set(999,3);
for (let v of set) {
console.log(v);
}
console.log("--------------------");
for(let [k,v] of map) {
console.log(k,v);
}

javascript遍历对象详细总结
1.原生javascript遍历
(1)for循环遍历
let array1 = ['a','b','c'];
for (let i = 0;i < array1.length;i++){
console.log(array1[i]); // a b c
}
(2)JavaScript 提供了 foreach() map() 两个可遍历 Array对象 的方
forEach和map用法类似,都可以遍历到数组的每个元素,而且参数一致;
Array.forEach(function(value , index , array){ //value为遍历的当前元素,index为当前索引,array为正在操作的数组
//do something
},thisArg) //thisArg为执行回调时的this值
不同点:
forEach() 方法对数组的每个元素执行一次提供的函数。总是返回undefined;
例子如下:

var array1 = [1,2,3,4,5]; var x = array1.forEach(function(value,index){ console.log(value); //可遍历到所有数组元素 return value + 10
});
console.log(x); //undefined 无论怎样,总返回undefined var y = array1.map(function(value,index){ console.log(value); //可遍历到所有数组元素 return value + 10
});
console.log(y); //[11, 12, 13, 14, 15] 返回一个新的数组

对于类似数组的结构,可先转换为数组,再进行遍历

let divList = document.querySelectorAll('div'); //divList不是数组,而是nodeList //进行转换后再遍历
[].slice.call(divList).forEach(function(element,index){
element.classList.add('test')
}) Array.prototype.slice.call(divList).forEach(function(element,index){
element.classList.remove('test')
}) [...divList].forEach(function(element,index){ //<strong>ES6写法</strong>
//do something
})

(3)for ··· in ··· / for ··· of ···
for...in
语句以任意顺序遍历一个对象的可枚举属性。对于每个不同的属性,语句都会被执行。每次迭代时,分配的是属性名
补充 : 因为迭代的顺序是依赖于执行环境的,所以数组遍历不一定按次序访问元素。 因此当迭代那些访问次序重要的 arrays 时用整数索引去进行 for
循环 (或者使用 Array.prototype.forEach()
或 for...of
循环) 。

let array2 = ['a','b','c']
let obj1 = {
name : 'lei',
age : '16'
} for(variable in array2){ //variable 为 index
console.log(variable ) //0 1 2
} for(variable in obj1){ //variable 为属性名
console.log(variable) //name age
}

ES6新增了 遍历器(Iterator)机制,为不同的数据结构提供统一的访问机制。只要部署了Iterator的数据结构都可以使用 for ··· of ··· 完成遍历操作 ( Iterator详解 : http://es6.ruanyifeng.com/#docs/iterator ),每次迭代分配的是 属性值
原生具备 Iterator 接口的数据结构如下:
Array Map Set String TypedArray 函数的arguments对象 NodeList对象

let array2 = ['a','b','c']
let obj1 = {
name : 'lei',
age : '16'
} for(variable of array2){ //<strong>variable 为 value</strong>
console.log(variable ) //'a','b','c'
} for(variable of obj1){ //<strong>普通对象不能这样用</strong>
console.log(variable) // 报错 : main.js:11Uncaught TypeError: obj1[Symbol.iterator] is not a function
}<br><br>let divList = document.querySelectorAll('div');<br><br>for(element of divlist){ //可遍历所有的div节点<br> //do something <br>}

如何让普通对象可以用for of 进行遍历呢? http://es6.ruanyifeng.com/#docs/iterator 一书中有详细说明了!
除了迭代时分配的一个是属性名、一个是属性值外,for in 和 for of 还有其他不同 (MDN文档: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of)
for...in循环会遍历一个object所有的可枚举属性。
for...of会遍历具有iterator接口的数据结构
for...in
遍历(当前对象及其原型上的)每一个属性名称,而 for...of遍历(当前对象上的)每一个属性值

Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {}; let iterable = [3, 5, 7];
iterable.foo = "hello"; for (let i in iterable) {
console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
} for (let i of iterable) {
console.log(i); // logs 3, 5, 7
}

js中forEach,for in,for of循环的用法的更多相关文章
- 关于JS中判断是数字和小数的正则表达式用法
关于JS中判断是数字和小数的正则表达式用法 正则表达式 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式可以是一个简单的字符,或一 ...
- js中forEach,for in,for of循环的用法详解
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- 十 js中forEach,for in,for of循环的用法
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i++) { console.log(i ...
- 原生JS中apply()方法的一个值得注意的用法
今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...
- JS中的call()、apply() 以及 bind()方法用法总结
JS中的call()方法和apply()方法用法总结 : 讲解: 调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域. function add(c,d){ return thi ...
- JS中forEach和map的区别
共同点: 1.都是循环遍历数组中的每一项. 2.forEach()和map()里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名函数中 ...
- js中 forEach 和 map 区别
共同点: 1.都是循环遍历数组中的每一项. 2.forEach()和map()里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名函数中 ...
- js中forEach无法跳出循环?
1. forEach() forEach() 方法从头至尾遍历数组,为每个元素调用指定的函数.如上所述,传递的函数作为forEach()的第一个参数.然后forEach()使用三个参数调用该 函数:数 ...
- js 中 forEach 和 map
共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名 ...
随机推荐
- 【BASIS系列】SAP BASIS模块-后台配置的传输
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP BASIS模块-后台配 ...
- LeetCode 144. Binary Tree Preorder Traversal 动态演示
先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...
- 21.线程,全局解释器锁(GIL)
import time from threading import Thread from multiprocessing import Process #计数的方式消耗系统资源 def two_hu ...
- LinkedList -链表集合
package cn.learn.collection; import java.util.LinkedList; import java.util.Queue; /* java.util.xxx A ...
- 如何创建linux虚拟机
一.安装配置linux虚拟机 第1步:运行"Vmware WorkStation",看到主页面. 第2步:创建新的虚拟机,新建虚拟机向导——典型(推荐). 第3步:选择稍后安装操作 ...
- "源文件名长度大于文件系统支持的长度无法删除"的解决方案
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * @auth ...
- java_第一年_JavaWeb(9)
JavaBean是一个遵循某种特定写法的Java类,有以下特点: 必需具有一个无参的构造函数 属性必需私有化 私有化的属性必需通过public类型的方法暴露给其它程序,其方法命名也有一定的规范 范例: ...
- python小学堂1
sun=0 start=1 while True: start1=start%2 if start1==1: sun = start + sun elif start1==0: sun=sun-sta ...
- SQL SERVER添加表注释、字段注释
--为字段添加注释 --Eg. execute sp_addextendedproperty 'MS_Description','字段备注信息','user','dbo','table','字段所属的 ...
- Node.js--fs 文件操作
process 模块 在使用的时候无需通过 require() 函数来加载该模块,可以直接使用. fs 模块,在使用的时候,必须通过 require() 函数来加载该模块,方可使用. 原因:proce ...