站長資訊網
        最全最豐富的資訊網站

        總結javascript中遍歷數組的幾種方法

        本篇文章給大家總結了一些javascript遍歷數組的幾種方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

        總結javascript中遍歷數組的幾種方法

        有幾種方法可以遍歷數組,下面將逐個羅列!

        while循環

        let index = 0; const array = [1, 2, 3, 4, 5];  while (index < array.length) {     console.log(array[index]);     index++; }

        總結javascript中遍歷數組的幾種方法

        for循環

        const array = [1,2,3,4,5]; for(let index=0;index<array.length;index++){     console.log(array[index]); } for(let index in array){     console.log(array[index]); }

        總結javascript中遍歷數組的幾種方法

        forEach

        const array=[1,2,3,4,5]; array.forEach(function(current_value,index,array){     console.log(`At index ${index} in array ${array} the value is ${current_value}`) })

        總結javascript中遍歷數組的幾種方法

        map

        最后一個構造很有用,但是不會返回新數組,這對于你的特定情況可能是不希望的。map通過對每個元素應用一個函數然后返回新數組來解決此問題。

        const array = [1,2,3,4,5]; const square = x =>Math.pow(x,2); const squares = array.map(square); console.log(`${array}`); console.log(`${squares}`)

        總結javascript中遍歷數組的幾種方法

        reduce

        reduce()方法對累加器和數組中的每個元素(從左到右)應用一個函數,以將其減小為單個值

        const array = [1,2,3,4,5]; const sum = (x,y) => x + y;  const array_sum = array.reduce(sum,0); console.log(`the sum of arrray:${array} is ${array_sum}`);

        總結javascript中遍歷數組的幾種方法

        filter

        根據布爾函數過濾篩選數組中的元素

        const array = [1,2,3,4,5]; const even = x => x%2 === 0; const even_array = array.filter(even); console.log(`even numbers in array ${array} : ${even_array}`);

        總結javascript中遍歷數組的幾種方法

        every

        得到了一個數組,想測試每個元素是否滿足給定條件

        const array = [1,2,3,4,5,8]; const under_six = x => x<6; if(array.every(under_six)){     console.log(`every elemnet in the array is less than 6`); } else{     console.log(`at least one element in the array was bigger than 6`); }

        總結javascript中遍歷數組的幾種方法

        some

        測試是否至少有一個元素與布爾函數匹配

        const array = [2,4,5,6,8]; const over_five = x => x>5;  if(array.some(over_five)){     console.log(`at least one element bigger than 5 was found`); } else{     console.log(`no element bigger than 5 was found`); }

        總結javascript中遍歷數組的幾種方法

        到此就結束啦,

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 亚洲精品无码成人片在线观看 | 久久丝袜精品中文字幕| 亚洲精品无码国产| 国产精品成人久久久久三级午夜电影 | 国产精品综合专区中文字幕免费播放| 精品人无码一区二区三区| 欧美日韩精品系列一区二区三区| 91精品国产91久久久久久蜜臀| 国产精品一久久香蕉国产线看观看| 亚洲精品乱码久久久久久中文字幕| 久99久无码精品视频免费播放| 亚洲国产精品自在线一区二区| 国产精品国产精品国产专区不卡 | 亚洲欧美日韩另类精品一区二区三区| 成人精品一区二区久久| 精品在线免费观看| 2021精品国产综合久久| 久久精品人人做人人爽97| 在线亚洲精品自拍| 亚洲AV成人精品日韩一区18p| 精品视频一区二区三区四区五区 | 97人妻无码一区二区精品免费| 亚洲AV永久无码精品水牛影视| 亚洲精品A在线观看| 麻豆精品三级全部视频| 久热这里只有精品99国产6| 精品国产黑色丝袜高跟鞋| 国产精品最新国产精品第十页 | 午夜精品久久久内射近拍高清| 精品欧美| 欧美精品亚洲日韩aⅴ| 精品久久人人做人人爽综合| 精品人妻少妇一区二区三区不卡| 精品久久国产一区二区三区香蕉 | 亚洲精品成人在线| 亚洲欧美日韩国产成人精品影院| 亚洲精品国产精品乱码不卞| 亚洲精品无码AV中文字幕电影网站| 亚洲AV成人精品网站在线播放 | 99精品在线免费| 黄床大片免费30分钟国产精品 |