科协网站建设建议,新手创业开什么店最好,食品网站建设实施方案,wordpress查看原图在 ES6#xff08;ECMAScript 2015#xff09;中#xff0c;新增了一些循环方法#xff0c;这些方法可以帮助我们更方便地遍历数组、字符串、Set、Map 等数据结构。本文将介绍一些常用的 ES6 循环方法。
for…of 循环
for…of 循环是一种遍历可迭代对象的方法#xff0c…在 ES6ECMAScript 2015中新增了一些循环方法这些方法可以帮助我们更方便地遍历数组、字符串、Set、Map 等数据结构。本文将介绍一些常用的 ES6 循环方法。
for…of 循环
for…of 循环是一种遍历可迭代对象的方法它可以遍历对象中的每个元素。可迭代对象包括数组、字符串、Set、Map 等。
以下是 for…of 循环的语法
for (let item of iterable) {// 循环体
}其中iterable 表示可迭代对象item 表示当前遍历到的元素。
以下是一个使用 for…of 循环遍历数组的例子
const arr [1, 2, 3, 4, 5];
for (let item of arr) {console.log(item);
}输出结果为
1
2
3
4
5Array.from() 方法
Array.from() 方法可以将类数组对象或可迭代对象转换为数组。它接受一个可迭代对象或类数组对象作为参数并返回一个新的数组。
以下是 Array.from() 方法的语法
Array.from(iterable[, mapFn[, thisArg]])其中iterable 表示可迭代对象或类数组对象mapFn 表示对每个元素进行处理的函数thisArg 表示处理函数中的 this 对象。
以下是一个使用 Array.from() 方法将字符串转换为数组的例子
const str hello;
const arr Array.from(str);
console.log(arr); // [h, e, l, l, o]Array.prototype.find() 方法
Array.prototype.find() 方法可以查找数组中符合条件的第一个元素。它接受一个回调函数作为参数该回调函数返回一个布尔值表示当前元素是否符合条件。如果找到符合条件的元素则返回该元素否则返回 undefined。
以下是 Array.prototype.find() 方法的语法
arr.find(callback[, thisArg])其中callback 表示回调函数thisArg 表示回调函数中的 this 对象。
以下是一个使用 Array.prototype.find() 方法查找数组中符合条件的元素的例子
const arr [1, 2, 3, 4, 5];
const result arr.find(item item 3);
console.log(result); // 4Array.prototype.findIndex() 方法
Array.prototype.findIndex() 方法可以查找数组中符合条件的第一个元素的索引。它接受一个回调函数作为参数该回调函数返回一个布尔值表示当前元素是否符合条件。如果找到符合条件的元素则返回该元素的索引否则返回 -1。
以下是 Array.prototype.findIndex() 方法的语法
arr.findIndex(callback[, thisArg])其中callback 表示回调函数thisArg 表示回调函数中的 this 对象。
以下是一个使用 Array.prototype.findIndex() 方法查找数组中符合条件的元素的索引的例子
const arr [1, 2, 3, 4, 5];
const index arr.findIndex(item item 3);
console.log(index); // 3Array.prototype.includes() 方法
Array.prototype.includes() 方法可以判断数组中是否包含指定元素。它接受一个参数表示要查找的元素。如果数组中包含该元素则返回 true否则返回 false。
以下是 Array.prototype.includes() 方法的语法
arr.includes(searchElement[, fromIndex])其中searchElement 表示要查找的元素fromIndex 表示查找的起始位置。
以下是一个使用 Array.prototype.includes() 方法判断数组中是否包含指定元素的例子
const arr [1, 2, 3, 4, 5];
const result arr.includes(3);
console.log(result); // true需要注意的是ES6 循环方法需要在支持 ES6 的环境中使用如果需要在不支持 ES6 的环境中使用可以使用 Babel 等工具进行转换。
结语
ES6 新增的循环方法为我们在遍历数组、字符串、Set、Map 等数据结构时提供了更加便捷的方式。在实际开发中我们可以根据需求选择合适的方法进行使用。 文章转载自: http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.gmgnp.cn.gov.cn.gmgnp.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.zmlbq.cn.gov.cn.zmlbq.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn