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

        總結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號
        主站蜘蛛池模板: 久久久久久一区国产精品| 亚洲精品无码久久久久久| 精品久久久久久无码人妻蜜桃 | 国产精品视频免费一区二区| 欧美午夜精品久久久久免费视| 精品国产乱码久久久久久浪潮| 久久精品成人免费网站| 国产精品偷伦视频观看免费| 尤物yw午夜国产精品视频| 精品久久久久久久中文字幕| 2024国产精品极品色在线| 99视频在线观看精品| 精品人妻va出轨中文字幕| 亚洲欧洲国产精品香蕉网| 欧美成人精品欧美一级乱黄一区二区精品在线| 国产精品网站在线观看免费传媒| 国产女人18毛片水真多18精品| 99re6在线精品免费观看| 久久精品中文騷妇女内射| 无码精品A∨在线观看中文| 亚洲日韩中文在线精品第一| 毛片a精品**国产| 精品乱码久久久久久夜夜嗨| 欧美日激情日韩精品| 欧美精品手机在线播放| 成人午夜精品视频在线观看| 国产精品免费观看调教网| 国产亚洲欧美精品久久久| 国产精品色内内在线播放| 国内精品人妻无码久久久影院| 日韩精品无码中文字幕一区二区| 亚洲伊人久久精品影院| 日韩精品乱码AV一区二区| 嫖妓丰满肥熟妇在线精品| 久久精品国产亚洲77777| 国内精品久久久久久99| 99精品国产一区二区三区2021| 国产精品毛片VA一区二区三区| 51午夜精品免费视频| 久久精品国产91久久综合麻豆自制| 久久精品国产91久久综合麻豆自制 |