麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > 28個Javascript數組方法匯總整理

28個Javascript數組方法匯總整理

來源:千鋒教育
發布人:qyf
時間: 2023-01-17 17:00:00 1673946000

28個Javascript數組方法匯總整理

  01、Array.map()

  返回一個新數組,其中包含對該數組中每個元素調用提供的函數的結果。

  const list = [ , , , ];

  list.map((??) => ); // [ , , , ]

  // Code

  const list = [1, 2, 3, 4];

  list.map((el) => el * 2); // [2, 4, 6, 8]

  02、Array.filter()

  返回一個新數組,其中包含通過所提供函數實現的測試的所有元素。

  const list = [ , , , ];

  list.filter((??) => ?? === ); // [ , ]

  // Code

  const list = [1, 2, 3, 4];

  list.filter((el) => el % 2 === 0); // [2, 4]

  03、Array.reduce()

  將數組減少為單個值。函數返回的值存儲在累加器中(結果/總計)。

  const list = [ , , , , ];

  list.reduce((??, ??) => ?? + ??); // + + + +

  // OR

  const list = [1, 2, 3, 4, 5];

  list.reduce((total, item) => total + item, 0); // 15

  04、Array.reduceRight()

  對數組的每個元素執行一個你提供的reducer 函數,從而產生一個輸出值(從右到左)。

  const list = [ , , , , ];

  list.reduceRight((??, ??) => ?? + ??); // + + + +

  // Code

  const list = [1, 2, 3, 4, 5];

  list.reduceRight((total, item) => total + item, 0); // 15

  05、Array.fill()

  用靜態值填充數組中的元素。

  const list = [ , , , , ];

  list.fill( ); // [ , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.fill(0); // [0, 0, 0, 0, 0]

  06、Array.find()

  返回數組中滿足提供的測試函數的第一個元素的值。否則返回未定義。

  const list = [ , , , , ];

  list.find((??) => ?? === ); //

  list.find((??) => ?? === ); // undefined

  // Code

  const list = [1, 2, 3, 4, 5];

  list.find((el) => el === 3); // 3

  list.find((el) => el === 6); // undefined

  07、Array.indexOf()

  返回可以在數組中找到給定元素的第一個索引,如果不存在則返回 -1。

  const list = [ , , , , ];

  list.indexOf( ); // 0

  list.indexOf( ); // -1

  // Code

  const list = [1, 2, 3, 4, 5];

  list.indexOf(3); // 2

  list.indexOf(6); // -1

  08、Array.lastIndexOf()

  返回可以在數組中找到給定元素的最后一個索引,如果不存在,則返回 -1。從 fromIndex 開始向后搜索數組。

  const list = [ , , , , ];

  list.lastIndexOf( ); // 3

  list.lastIndexOf( , 1); // 0

  // Code

  const list = [1, 2, 3, 4, 5];

  list.lastIndexOf(3); // 2

  list.lastIndexOf(3, 1); // -1

  09、Array.findIndex()

  返回數組中滿足提供的測試函數的第一個元素的索引。否則,返回 -1。

  const list = [ , , , , ];

  list.findIndex((??) => ?? === ); // 0

  // You might be thinking how it's different from `indexOf`

  const array = [5, 12, 8, 130, 44];

  array.findIndex((element) => element > 13); // 3

  // OR

  const array = [{

  id:

  }, {

  id:

  }, {

  id:

  }];

  array.findIndex((element) => element.id === ); // 2

  10、Array.includes()

  如果給定元素存在于數組中,則返回 true。

  const list = [ , , , , ];

  list.includes( ); // true

  // Code

  const list = [1, 2, 3, 4, 5];

  list.includes(3); // true

  list.includes(6); // false

  11、Array.pop()

  從數組中刪除最后一個元素并返回該元素。

  const list = [ , , , , ];

  list.pop(); //

  list; // [ , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.pop(); // 5

  list; // [1, 2, 3, 4]

  12、Array.push()

  將新元素追加到數組的末尾,并返回新的長度。

  const list = [ , , , , ];

  list.push( ); // 5

  list; // [ , , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.push(6); // 6

  list; // [1, 2, 3, 4, 5, 6]

  13、Array.shift()

  從數組中刪除第一個元素并返回該元素。

  const list = [ , , , , ];

  list.shift(); //

  list; // [ , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.shift(); // 1

  list; // [2, 3, 4, 5]

  14、Array.unshift()

  將新元素添加到數組的開頭,并返回新長度。

  const list = [ , , , , ];

  list.unshift( ); // 6

  list; // [ , , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.unshift(0); // 6

  list; // [0, 1, 2, 3, 4, 5]

  15、Array.splice()

  通過刪除或替換現有元素和/或在適當位置添加新元素來更改數組的內容。

  const list = [ , , , , ];

  list.splice(1, 2); // [ , ]

  list; // [ , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.splice(1, 2); // [2, 3]

  list; // [1, 4, 5]

  16、Array.slice()

  將數組的一部分的淺拷貝返回到從開始到結束(不包括結束)選擇的新數組對象中,原始數組不會被修改。

  const list = [ , , , , ];

  list.slice(1, 3); // [ , ]

  list; // [ , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.slice(1, 3); // [2, 3]

  list; // [1, 2, 3, 4, 5]

  17、Array.join()

  將數組的所有元素連接成一個字符串。

  const list = [ , , , , ];

  list.join('??'); // " ?? ?? ?? ?? "

  // Code

  const list = [1, 2, 3, 4, 5];

  list.join(', '); // "1, 2, 3, 4, 5"

  18、Array.reverse()

  反轉數組中元素的順序。

  const list = [ , , , , ];

  list.reverse(); // [ , , , , ]

  list; // [ , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.reverse(); // [5, 4, 3, 2, 1]

  list; // [5, 4, 3, 2, 1]

  19、Array.sort()

  對數組的元素進行就地排序并返回該數組。默認排序順序是根據字符串 Unicode 代碼點。

  const list = [ , , , , ];

  list.sort(); // [ , , , , ]

  // This make more sense

  const array = ['D', 'B', 'A', 'C'];

  array.sort(); // ['A', 'B', 'C', 'D']

  // OR

  const array = [4, 1, 3, 2, 10];

  array.sort(); // [1, 10, 2, 3, 4]

  array.sort((a, b) => a - b); // [1, 2, 3, 4, 10]

  20、Array.some()

  如果數組中至少有一個元素通過了提供的函數實現的測試,則返回 true。

  const list = [ , , , , ];

  list.some((??) => ?? === ); // true

  list.some((??) => ?? === ); // false

  // Code

  const list = [1, 2, 3, 4, 5];

  list.some((el) => el === 3); // true

  list.some((el) => el === 6); // false

  21、Array.every()

  如果數組中的所有元素都通過了提供的函數實現的測試,則返回 true。

  const list = [ , , , , ];

  list.every((??) => ?? === ); // false

  const list = [ , , , , ];

  list.every((??) => ?? === ); // true

  // Code

  const list = [1, 2, 3, 4, 5];

  list.every((el) => el === 3); // false

  const list = [2, 4, 6, 8, 10];

  list.every((el) => el%2 === 0); // true

  22、Array.from()

  從類數組或可迭代對象創建一個新數組。

  const list = ;

  Array.from(list); // [ , , , , ]

  const set = new Set([' ', ' ', ' ', ' ', ' ']);

  Array.from(set); // [ , , ]

  const range = (n) => Array.from({ length: n }, (_, i) => i + 1);

  console.log(range(10)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  23、Array.of()

  使用可變數量的參數創建一個新數組,而不管參數的數量或類型。

  const list = Array.of( , , , , );

  list; // [ , , , , ]

  // Code

  const list = Array.of(1, 2, 3, 4, 5);

  list; // [1, 2, 3, 4, 5]

  24、Array.isArray()

  如果給定值是一個數組,則返回 true。

  Array.isArray([ , , , , ]); // true

  Array.isArray( ); // false

  // Code

  Array.isArray([1, 2, 3, 4, 5]); // true

  Array.isArray(5); // false

  25、Array.at()

  返回指定索引處的值。

  const list = [ , , , , ];

  list.at(1); //

  // Return from last

  list.at(-1); //

  list.at(-2); //

  // Code

  const list = [1, 2, 3, 4, 5];

  list.at(1); // 2

  list.at(-1); // 5

  list.at(-2); // 4

  26、Array.copyWithin()

  復制數組中的數組元素。返回修改后的數組。

  const list = [ , , , , ];

  list.copyWithin(1, 3); // [ , , , , ]

  const list = [ , , , , ];

  list.copyWithin(0, 3, 4); // [ , , , , ]

  // Code

  const list = [1, 2, 3, 4, 5];

  list.copyWithin(0, 3, 4); // [4, 2, 3, 4, 5]

  注意:第一個參數是開始復制元素的目標。第二個參數是開始復制元素的索引。第三個參數是停止復制元素的索引。

  27、Array.flat()

  返回一個新數組,其中所有子數組元素遞歸連接到指定深度。

  const list = [ , , [ , , ]];

  list.flat(Infinity); // [ , , , , ]

  // Code

  const list = [1, 2, [3, 4, [5, 6]]];

  list.flat(Infinity); // [1, 2, 3, 4, 5, 6]

  28、Array.flatMap()

  返回通過將給定的回調函數應用于數組的每個元素而形成的新數組,

  const list = [ , , [ , , ]];

  list.flatMap((??) => [??, ?? + ?? ]); // [ , , , , , , , , , ]

  // Code

  const list = [1, 2, 3];

  list.flatMap((el) => [el, el * el]); // [1, 1, 2, 4, 3, 9]

  總結

  以上就是我今天跟你分享的28個Javascript 數組方法,希望對你有幫助。如果你覺得有用的話,請記得點贊我,關注我,并將其分享給你身邊的朋友,也許能夠幫助到他。

  - End -

tags:
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 免费一级做a爰片久久毛片潮喷| 毛片1| 免费很黄很色裸乳在线观看| 热久久国产精品| 日韩在线一区二区三区免费视频| 再深一点灬舒服灬太大了| ririai66在线观看视频| 免费中韩高清无专码区2021| 国内a级毛片免费···| 抵在洗手台挺进撞击bl| 麻豆精品视频入口| 极品丝袜系列列表| 香港黄页亚洲一级| 开始疼痛的小小花蕾3| 精品无人区一区二区三区a| 久久婷婷国产综合精品| 美女扒开内裤羞羞网站| 亚洲免费福利视频| 1024视频基地| 精品久久久久久无码中文字幕| 国产免费久久精品99久久| 欧美一区二区三区久久久人妖 | 波多野结衣在线免费电影| 欧美一级片手机在线观看| 国产精品日本一区二区在线播放 | 91蝌蚪在线视频| 国产jav| 最近手机中文字幕1页| 久久99亚洲网美利坚合众国| 国产一区二区小早川怜子| 4408私人影院| 欧美aaaaaaaaa| 中文字幕一区二区三区精彩视频 | 国内一级黄色片| аⅴ中文在线天堂| 精品一区二区三区自拍图片区 | 免费a级毛片18以上观看精品| 女人爽小雪又嫩又紧| 四虎永久在线精品免费影视| zoom和okzoom在线视频| 天下第一社区视频welcome|