日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ES5总结1:数组Array新特性最全最精简的详解

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES5总结1:数组Array新特性最全最精简的详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2個位置方法:indexOf? lastIndexOf

5個迭代方法:forEach?? ? ??every? some? ? ? ?filter? map? ? ? ??

2個高階函數:reduce? reduceRight? (可用于數組求和)

API:?

1、indexOf(searchElement: T, fromIndex?: number): number;? // 返回被查找的元素在數組中的索引index,判斷標準為恒等于“===

? ? ?lastIndexOf(searchElement: T, fromIndex?: number): number; // 從后往前查找,返回元素在數組中的索引index

var arr = [1,2,3,4,5,4,3,2,1]; // 一個參數 var index = arr.indexOf(4); // 3 alert(index); // 兩個參數 index = arr.indexOf(4, 4); alert(index); // 5 alert(arr.indexOf(true)); // -1 alert(arr.lastIndexOf(3)); // 6

2、(1) forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;

? ? ? ? ? ? ? ?對于數組每一個元素執行一個函數

? ? ?(2) every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;

? ? ? ? ? ? ? 對于數組每一個元素執行一個函數,如果都返回true,則最終返回true,否則返回false。

? ?(3)some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;

? ? ? ? ? ? ? 對于數組每一個元素執行一個函數,如果有一個返回true,則最終返回true,否則返回false?

? ? ?(4)filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];

? ? ? ? ? ? ? 對于數組每一個元素執行一個函數,用指定的條件過濾,返回原數組中所有滿足條件的元素組成的新數組。

? ? ?(5)map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];

? ? ? ? ? ? ? 對于數組每一個元素執行一個函數,函數返回值組成新數組并返回

// 對于數組每一個元素執行一個函數 arr.forEach(function (value, index, array) {// ... })// 對于數組每一個元素執行一個函數,如果都返回true,則最終返回true,否則返回false var result = arr.every(function (value, index, array) {return value > 2; }, 2); alert(result); // false// 對于數組每一個元素執行一個函數,如果有一個返回true,則最終返回true,否則返回false var someResult = arr.some(function (value, index, array) {return value >= 5; }, 2); // alert(someResult); // true// 對于數組每一個元素執行一個函數,用指定的條件過濾,返回所有滿足條件的元素組成的新數組 var filterResult = arr.filter(function (value,index,array) {return value > 2; }); console.log(filterResult); // [3, 4, 5, 4, 3]// 對于數組每一個元素執行一個函數,函數返回值組成新數組并返回 var mapResult = arr.map(function (value,index,array) {return value * 2; }); console.log(mapResult); // [2,4,6,8,10,8,6,4,2]

3、reduce和reduceRight高階函數

reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;

reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;

從數組中第2個元素開始執行函數,把上一次的函數返回值作為本次函數的第一個參數的值previousValue,(previousValue初始值默認為原數組第一個元素的值,也可通過initialValue參數指定);reduceRight從右邊開始遍歷(同樣從第2個元素開始)

// 從數組中第2個元素開始執行函數,把上一次的函數返回值作為本次函數的第一個參數的值previousValue,(previousValue初始值默認為原數組第一個元素的值) var reduceResult = arr.reduce(function (previousValue, currentValue, currentIndex, array) {return previousValue + currentValue; // 此時相當于數組求和 }); alert(reduceResult); // 25 原數組[1,2,3,4,5,4,3,2,1]

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的ES5总结1:数组Array新特性最全最精简的详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。