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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript的数组常用方法

發(fā)布時(shí)間:2023/12/31 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript的数组常用方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)組常用方法

1. push

=> 語法 arr.push(數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, ...) => 作用 把所有的參數(shù)按照順序追加到數(shù)組的末尾 => 返回值,追加以后數(shù)組的長度 => 直接操作原始數(shù)組

2. pop

=> 語法 數(shù)組.pop() => 作用 刪除數(shù)組的最后一個(gè)數(shù)據(jù) => 返回值 被刪除的數(shù)據(jù) => 直接操作原始數(shù)組

3. unshift()

=> 語法 數(shù)組.unshift(數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, ...) => 作用 從數(shù)組最前面插入一些數(shù)據(jù) => 返回值 插入后的數(shù)組長度 => 直接操作原始數(shù)組

4. shift()

=> 語法 數(shù)組.unshift() => 作用 刪除數(shù)組的最前面一個(gè)元素 => 返回值 被刪除的數(shù)據(jù) => 直接操作原始數(shù)組

5. reverse()

=> 語法 數(shù)組.reverse() => 作用 反轉(zhuǎn)數(shù)組 => 返回值 反轉(zhuǎn)后的數(shù)組 => 直接操作原始數(shù)組

6. sort()

=> 語法 數(shù)組.sort()1. arr.sort()-> 按照每一個(gè)數(shù)據(jù)中的每一位數(shù)組的ASCII碼進(jìn)行排列2. arr.sort(function (a, b) {return a -b})-> 從小到大排2. arr.sort(function (a, b) {return b -a})-> 從大到小排 => 作用 數(shù)組排序 => 返回值 排序后的數(shù)組 => 直接操作原始數(shù)組

7. splice()

=> 語法 數(shù)組.splice(開始索引,多少個(gè))1. 數(shù)組.splice(開始索引,多少個(gè))-> 從開始索引,截取多少個(gè)-> 第二個(gè)參數(shù)可以不寫,直接到末尾2. 數(shù)組.splice(開始索引,多少個(gè),替換數(shù)組1,替換數(shù)組2,替換數(shù)組3, ...)-> 把替換數(shù)據(jù)按照順序插入到你街區(qū)的位置-> 注意 從那個(gè)位置開始索引刪除,就替換數(shù)據(jù)的第一個(gè)就插入到哪個(gè)位置 => 作用 1. 截取數(shù)組2. 替換新內(nèi)容 => 返回值 一定是一個(gè)數(shù)組 => 如果你截取一個(gè)數(shù)據(jù),數(shù)組里面有一個(gè) => 如果你一個(gè)都不截取,那么是一個(gè)空數(shù)組 => 直接操作原始數(shù)組

8. concat()

=> 語法 數(shù)組.concat(數(shù)據(jù)1,數(shù)據(jù)2) => 作用 -> 如果參數(shù)是數(shù)組,那么把數(shù)組拆開,里面每一項(xiàng)追加到原數(shù)組后面-> 如果是參數(shù)數(shù)據(jù),那么直接追加 => 返回值 追加好的數(shù)組 => 不改變原始數(shù)組

9. slice()

=> 語法1. 數(shù)組.slice(開始索引, 結(jié)束索引) [)第一個(gè)參數(shù)可以不寫,表示從頭第二個(gè)參數(shù)可以不寫,表示到尾2. 數(shù)組.slice(開始索引, 結(jié)束索引) [) -> 參數(shù)可以寫一個(gè)負(fù)整數(shù)-> length - 負(fù)數(shù) => 作用 獲取數(shù)組里的某些數(shù)據(jù) => 返回值 一個(gè)數(shù)組-> 如果你獲取多個(gè)數(shù)據(jù),數(shù)組里面有多個(gè)-> 如果你獲取一個(gè)數(shù)據(jù),數(shù)組里面有一個(gè)-> 如果你一個(gè)都不獲取,那么是個(gè)空數(shù)組 => 不改變原始數(shù)組

10. join()

=> 語法:數(shù)組.join("連接符號")-> 不傳遞參數(shù)是按照(,)連接-> 你傳遞什么按照什么連接 => 作用:把數(shù)字里面得每一個(gè)數(shù)據(jù)使用連接符號連接在一起 => 返回值: 是一個(gè)連接好的內(nèi)容,是一個(gè)String 類型

11. indexOf()

=> 語法:-> 數(shù)組.indexOf(數(shù)據(jù))-> 數(shù)組.indexOf(數(shù)據(jù), 開始索引) 從哪個(gè)索引開始查找 => 作用: 正向查找數(shù)組里面指定這個(gè)數(shù)據(jù)的索引 => 返回值: -> 如果有這個(gè)數(shù)據(jù),是第一個(gè)滿足條件的數(shù)據(jù)的索引-> 如果沒有這個(gè)數(shù)據(jù),那么是 -1

12. lastIndexOf()

=> 語法:-> 數(shù)組.lastIndexOf(數(shù)據(jù))-> 數(shù)組.lastIndexOf(數(shù)據(jù), 開始索引) 從哪個(gè)索引開始往前查找 => 作用: 反向查找數(shù)組里面指定這個(gè)數(shù)據(jù)的索引 => 返回值: -> 如果有這個(gè)數(shù)據(jù),是第一個(gè)滿足條件的數(shù)據(jù)的索引-> 如果沒有這個(gè)數(shù)據(jù),那么是 -1 -> 雖然是從后往前索引,但是索引還是正常索引

13. forEach()

=> 語法:數(shù)組.forEach(function (item, index, arr){})-> item 數(shù)組的每一項(xiàng)-> index 數(shù)組的索引-> arr 原始數(shù)組 => 作用:取代for循環(huán)的作用,遍歷數(shù)組 => 沒有返回值

13. map()

=> 語法:數(shù)組.map(function (item, index, arr){})-> item 數(shù)組的每一項(xiàng)-> index 數(shù)組的索引-> arr 原始數(shù)組 => 作用:映射數(shù)組 => 返回值: -> 新的數(shù)組-> 里面是對原始數(shù)組每一個(gè)數(shù)據(jù)的操作-> 返回值數(shù)組,一定和原始數(shù)組長度一樣 => 不改變原始數(shù)組

對應(yīng)數(shù)組操作代碼

// 1.pushvar arr = ["hello", "world", "你好", "世界"]var res = arr.push("新來的", "新來的2", "新來的3")console.log(res);console.log(arr);// 2.popvar arr = ["hello", "world", "你好", "世界"]var res = arr.pop();console.log(res);console.log(arr);// 3. unshiftvar arr = ["hello", "world", "你好", "世界"]var res = arr.unshift("新來的1", "新來的2");console.log(res);console.log(arr);// 4. shiftvar arr = ["hello", "world", "你好", "世界"]var res = arr.shift("新來的1", "新來的2");console.log(res);console.log(arr);// 5. reverse()var arr = ["hello", "world", "你好", "世界"]var res = arr.reverse();console.log(res);console.log(arr);// 6. sort()var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]var res = arr.sort();console.log(res);console.log(arr);var res = arr.sort(function(a, b) {return a - b})console.log(res);console.log(arr);var res = arr.sort(function(a, b) {return b - a})console.log(res);console.log(arr);// 7. splice()var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]// var res = arr.splice(1, 3);// console.log(res);// console.log(arr);var res = arr.splice(1, 0, "新來的", "新來的二");console.log(res);console.log(arr);// 8. concat()var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]var res = arr.concat([10, 20], [30, 40], 100);console.log(res);console.log(arr);// 9. slicevar arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]var res = arr.slice(1, 3);console.log(res);console.log(arr);var res2 = arr.slice(-8, -6);console.log(res2);console.log(arr);// 10. join()var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]var res = arr.join("@-@");var res = arr.join();console.log(res);console.log(arr);// 11. indexOf()var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]var res = arr.indexOf(11);console.log(res);console.log(arr);var res = arr.indexOf(11, 4);console.log(res);console.log(arr);// 12. LastIndexOf()var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]var res = arr.lastIndexOf(11);console.log(res);console.log(arr);var res = arr.lastIndexOf(11, 5);console.log(res);console.log(arr);// 13. forEach()var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]let res = arr.forEach(function(item, index, arr) {// 這個(gè)函數(shù),會根據(jù)數(shù)組里面有多少個(gè)數(shù)據(jù)執(zhí)行多少回// console.log("我執(zhí)行了")// item 分別是這個(gè)數(shù)組里面的每一項(xiàng)console.log(item);console.log(index);console.log(arr);})// 14. map()var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]var res = arr.map(function(item, index, arr) {console.log(item, "-----", index, "-----", arr);return item * 1.3;})console.log(res);

總結(jié)

以上是生活随笔為你收集整理的JavaScript的数组常用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。