JavaScript 中的六種循環(huán)方法
對(duì)于數(shù)值索引的數(shù)組來(lái)說(shuō),可以使用標(biāo)準(zhǔn)的for循環(huán)來(lái)遍歷值
const arr=[1,2,3,4];for(let i=0;i<arr.length;i++){ console.log(i);}2.for...in循環(huán)
for...in循環(huán)可以用來(lái)遍歷對(duì)象的可枚舉屬性列表(包括原型鏈上的屬性)
const myObject={};Object.defineProperty(myobject,'a',{ //可枚舉enumerable:true, value:2,})Object.defineProperty(myobject,'b',{ //不可枚舉enumerable:false, value:2,})for(let k in myObject){ console.log(k,myObject[k])// a 2}//使用for...in循環(huán)是無(wú)法直接獲得屬性值的,因?yàn)樗鼘?shí)際遍歷的是對(duì)象中的所有可枚舉屬性,//所以你需要手動(dòng)獲得屬性值.
在數(shù)組上應(yīng)用for...in循環(huán),不僅僅會(huì)包含所有數(shù)值索引,還會(huì)包含所有可枚舉屬性.
所以最好在對(duì)象上應(yīng)用for...in循環(huán)。如果要遍歷數(shù)組最好使用傳統(tǒng)的for循環(huán)來(lái)遍歷.
3.for...of循環(huán)1.ES6新增的for...of循環(huán)
const arr=[1,2,3];for(let value of arr){ console.log(value) //1 //2 //3}
for...of循環(huán)首先會(huì)向所有被訪問(wèn)的對(duì)象請(qǐng)求一個(gè)迭代器對(duì)象,然后通過(guò)調(diào)用迭代器對(duì)象的next()方法來(lái)遍歷所有返回值
在數(shù)組中有內(nèi)置的@@iterator,因此for...of可以直接應(yīng)用在數(shù)組上。
使用內(nèi)置的@@iterator遍歷數(shù)組
const arr=[1,2,3];//獲取數(shù)組中的iterator對(duì)象:使用ES6中的符號(hào)Symbol.iterator來(lái)獲取對(duì)象的@@iteraotr內(nèi)部屬性.//@@iterator本身不是一個(gè)迭代器,而是一個(gè)返回迭代器對(duì)象的函數(shù)。const it=arr[Symbol.iterator]();it.next(); //{value:1,done:false}it.next(); //{value:2,done:false}it.next(); //{value:3,done:false}it.next(); //{done:true}//調(diào)用迭代器的next()方法會(huì)返回形式為{value:..,done:..}的值;//value為當(dāng)前的值,done是一個(gè)布爾值,表示是否還存在可以遍歷的值
2.給對(duì)象定義@@iterator
const myObject={ a:2, b:3}Object.defineProperty(myObject,Symbol.iterator,{enumerable:false, writeable:false, configurable:true, value:function(){ let o=this; let idx=0; //對(duì)象中的屬性數(shù)組 let ks=Object.keys(o); return{ value:o[ks[idx++]], done:(idx>ks.length); } }})const it=myObject[Symbol.iterator]();it.next(); //{value:2,done:false}it.next(); //{value:3,done:false}it.next(); //{done:true}for(let value of myObject){console.log(value);}// 2// 34.foreach(...)
**forEach()** 方法對(duì)數(shù)組的每個(gè)元素執(zhí)行一次給定的函數(shù)。
const arr = [’a’, ’b’, ’c’];arr.forEach(element => console.log(element));// a// b// c
arr.forEach(callback(currentValue [,index [,array]])[,thisArg])5.some(...)
some()是對(duì)數(shù)組中每一項(xiàng)運(yùn)行給定函數(shù),如果該函數(shù)對(duì)任一項(xiàng)返回true,則返回true。
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.some( function( item, index, array ){ console.log( ’item=’ + item + ’,index=’+index+’,array=’+array ); return item > 3; })); // item=1,index=0,array=1,2,3,4,5,6// item=2,index=1,array=1,2,3,4,5,6// item=3,index=2,array=1,2,3,4,5,6// item=4,index=3,array=1,2,3,4,5,6// true6.every(...)
every()是對(duì)數(shù)組中每一項(xiàng)運(yùn)行給定函數(shù),如果該函數(shù)對(duì)每一項(xiàng)返回true,則返回true。
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.every( function( item, index, array ){ console.log( ’item=’ + item + ’,index=’+index+’,array=’+array ); return item > 3; }));// item=1,index=0,array=1,2,3,4,5,6// false
以上就是JavaScript 中的六種循環(huán)方法的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 循環(huán)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. IntelliJ IDEA刪除類的方法步驟2. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼3. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析4. Django視圖類型總結(jié)5. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式6. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼7. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程8. JSP中Servlet的Request與Response的用法與區(qū)別9. Struts2獲取參數(shù)的三種方法總結(jié)10. vue cli4下環(huán)境變量和模式示例詳解
