JavaScript 中的六種循環(huán)方法
Javascript中的遍歷循環(huán)
1.for循環(huán)
對于數(shù)值索引的數(shù)組來說,可以使用標(biāo)準(zhǔn)的for循環(huán)來遍歷值
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)可以用來遍歷對象的可枚舉屬性列表(包括原型鏈上的屬性)
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)是無法直接獲得屬性值的,因為它實際遍歷的是對象中的所有可枚舉屬性, //所以你需要手動獲得屬性值.
在數(shù)組上應(yīng)用for...in循環(huán),不僅僅會包含所有數(shù)值索引,還會包含所有可枚舉屬性.
所以最好在對象上應(yīng)用for...in循環(huán)。如果要遍歷數(shù)組最好使用傳統(tǒng)的for循環(huán)來遍歷.
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)首先會向所有被訪問的對象請求一個迭代器對象,然后通過調(diào)用迭代器對象的next()方法來遍歷所有返回值
在數(shù)組中有內(nèi)置的@@iterator,因此for...of可以直接應(yīng)用在數(shù)組上。
使用內(nèi)置的@@iterator遍歷數(shù)組
const arr=[1,2,3]; //獲取數(shù)組中的iterator對象:使用ES6中的符號Symbol.iterator來獲取對象的@@iteraotr內(nèi)部屬性. //@@iterator本身不是一個迭代器,而是一個返回迭代器對象的函數(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()方法會返回形式為{value:..,done:..}的值; //value為當(dāng)前的值,done是一個布爾值,表示是否還存在可以遍歷的值
2.給對象定義@@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; //對象中的屬性數(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 // 3
4.foreach(...)
**forEach()**
方法對數(shù)組的每個元素執(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()是對數(shù)組中每一項運行給定函數(shù),如果該函數(shù)對任一項返回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 // true
6.every(...)
every()是對數(shù)組中每一項運行給定函數(shù),如果該函數(shù)對每一項返回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)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Bootstrap源碼解讀標(biāo)簽、徽章、縮略圖和警示框(8)
這篇文章主要源碼解讀了標(biāo)簽、徽章、縮略圖和警示框,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript根據(jù)json生成html表格的示例代碼
這篇文章主要介紹了JavaScript根據(jù)json生成html表格的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10js this函數(shù)調(diào)用無需再次抓獲id,name或標(biāo)簽名
this就是你當(dāng)前要執(zhí)行的js所抓獲的節(jié)點,這樣在js里就可以不用document.getElement之類的寫法來抓獲id,name或標(biāo)簽名,具體示例如下2014-03-03js 動態(tài)添加標(biāo)簽(新增一行,其實很簡單,就是幾個函數(shù)的應(yīng)用)
把所有代碼拷下另存為一個html文件,在瀏覽器中打開,點擊“新增一行”按鈕就可以,以下是對js函數(shù)的解釋2009-03-03JavaScript中數(shù)組隨機排序的實現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了JavaScript中數(shù)組隨機排序的實現(xiàn),主要是利用原地算法和sort/shuffle算法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-11-11