當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript的数组常用方法
生活随笔
收集整理的這篇文章主要介紹了
JavaScript的数组常用方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)組常用方法
1. push
=> 語法 arr.push(數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, ...) => 作用 把所有的參數(shù)按照順序追加到數(shù)組的末尾 => 返回值,追加以后數(shù)組的長(zhǎng)度 => 直接操作原始數(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ù)組長(zhǎng)度 => 直接操作原始數(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ù)組 => 不改變?cè)紨?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ù)組 => 不改變?cè)紨?shù)組10. join()
=> 語法:數(shù)組.join("連接符號(hào)")-> 不傳遞參數(shù)是按照(,)連接-> 你傳遞什么按照什么連接 => 作用:把數(shù)字里面得每一個(gè)數(shù)據(jù)使用連接符號(hào)連接在一起 => 返回值: 是一個(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ù),那么是 -112. 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ù)組-> 里面是對(duì)原始數(shù)組每一個(gè)數(shù)據(jù)的操作-> 返回值數(shù)組,一定和原始數(shù)組長(zhǎng)度一樣 => 不改變?cè)紨?shù)組對(duì)應(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ù),會(huì)根據(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的数组常用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ps4可以玩ps3游戏吗
- 下一篇: 人人通空间如何上传视频(我又登录上了人人