js函數返回值類型?作用:方法用于連接兩個或多個數組改變原數組:否返回值:拼接後的新數組參數:可以是具體值,也可以是一個數組,今天小編就來說說關于js函數返回值類型?下面更多詳細答案一起來看看吧!
作用:方法用于連接兩個或多個數組改變原數組:否返回值:拼接後的新數組參數:可以是具體值,也可以是一個數組
const arr1 = [2, 3, 4];const arr2 = [6, 7, 8]; const newArr = arr1.concat('a', arr2, 9);console.log(arr1); // [2, 3, 4] console.log(newArr); // [2, 3, 4, 'a', 6, 7, 8, 9]複制代碼
另外 toString() y也可以 直接轉字符串
2 join()
作用:數組轉字符串 改變原數組:否返回值:轉換後的新數組參數:傳入的參數作為分隔符
let arr = [1, 2, 3]console.log(arr.join('-'))//1-2-3console.log(arr) [1,2,3]複制代碼
3 slice()
作用:取數組元素 改變原數組:否返回值:新數組參數:數值 無:截取整個數據組 返回截取的數組1個:以該值為索引 ,截取包括該索引及之後的元素 返回截取的數組2個:slice(a,b) 截取 [a,b)的元素 返回截取的數組
console.log([1, 2, 3].slice(1, 2))// 2複制代碼
2 增删改查及堆棧方法
4 splice()
改變原數組:是 用法有很多 傳參也比較麻煩 最常用的有:1個參數 a 删除 [a,+∞) 的元素 返回删除部分數組2個 (a,b) 删除 包含索引a 開始往後 b 個元素 返回删除部分數組
let arr = [1, 2, 3, 4]console.log(arr.splice(1, 2)) // [2,3 ]console.log(arr) // [1, 4]複制代碼
多個: (a,b,x1,x2…) 删除 包含索引a 開始往後 b 個元素 ,并在原來删除位置插入x1,x2…xn
const arr = [1, 2, 3, 4]console.log(arr.splice(1, 2, 'a', 'b', 'c')) // [2,3 ]console.log(arr) // [1,a, b, c, 4]複制代碼
5 pop()
作用:用于删除數組的最後一個元素并返回删除的元素。 改變原數組:是
6 push()
作用:從數組末尾向數組添加元素,可以添加一個或多個元素。改變原數組:是返回值: 數組長度
const arr = [1, 2, 3, 4]console.log(arr.push('a', 'b', 'c')) // 7console.log(arr) // [1, 2, 3, 4, 'a', 'b', 'c']複制代碼
7 unshift()
作用:可向數組的開頭添加一個或更多元素,并返回新的長度。改變原數組:是
8 shift()
作用:用于删除數組的最開頭一個元素并返回删除的元素改變原數組:是
3 排序方法
9 reverse()
作用:将數組反序 返回新數組。改變原數組:是
10 sort(compare)
排序順序可以是字母或數字,并按升序或降序。 默認排序順序為按字母升序。參數 : 是一個函數 沒有參數(沒指明函數) 按升序排列若指明函數 按函數中的返回值來
//比較函數—升序 let compare = (x, y) => { if (x < y) { return -1 } else if (x > y) { return 1 } else { return 0 } } //比較函數—降序 let compare = (x, y) => { if (x < y) { return 1 } else if (x > y) { return -1 } else { return 0 } } //簡化 //升序 arr.sort((a, b) => { return a - b; }) //降序 arr.sort((a, b) => { return b - a;複制代碼
按照函數返回值 :返回值:小于 0 ,那麼 a 會被排列到 b 之前。 等于 0 , a 和 b 的相對位置不變。 大于 0 , b 會被排列到 a 之前 由此也可以實現數組亂序
function compare(a, b) { return 0.5 - Math.random() }let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']arr.sort(compare)複制代碼
3 叠代(或遍曆)方法
11 forEach()
用于調用數組的每個元素,并将元素傳遞給回調函數。 該方法沒有且不能手動指定返回值
let sum = 0const arr = [1, 2, 3, 4]arr.forEach((item, index, arr) => { sum += v})console.log(sum) //10複制代碼
12 every()
用于判斷數組中每一項是否都滿足條件,隻有所有項都滿足條件,才會返回true。
const arr = [1, 2, 3, 4]let res = arr.every((item, index, arr) => { return item > 2})console.log(res)//false複制代碼
13 some()
用于判斷數組中是否有一項是否都滿足條件,有項都滿足條件,就返回true。
14 filter()
創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。該方法不會改變原數組
const arr = [1, 2, 3, 4]let res = arr.filter((item, index, arr) => { if (item > 2) return item})console.log(res)//[3,4]複制代碼
14 map()
方法返回一個新數組,數組中的元素為原始數組元素調用函數處理後的值。 map() 方法按照原始數組元素順序依次處理元素。 該方法不會改變原數組
const arr = [1, 2, 3, 4]let res = arr.map((item, index, arr) => { return item * 2})console.log(res)//[2,4,6,8]複制代碼
作為一個映射 數組有長度為多長,返回處理後的新數組就有多長 如
const arr = [1, 2, 3, 4]let res = arr.map((item, index, arr) => { if (item > 2) return item})console.log(res)//複制代碼
15 reduce()
方法接收一個函數作為累加器,有四個基本參數,數組中的每個值(從左到右)開始縮減,最終計算為一個值。 該方法的用法很多 很多數組的操作能實現 第二個參數為acc的初始值 ,前一次的返回結果會作為下一次累計器的初始值
const arr = [1, 2, 3, 4]let res = arr.reduce((acc, item, index, arr) => { return acc + item}, 0)console.log(res) //10複制代碼
3 其他方法
16 indexOf()
可返回數組中某個指定的元素位置。
該方法将從頭到尾地檢索數組,看它是否含有對應的元素。開始檢索的位置在數組 start 處或數組的開頭(沒有指定 start 參數時)。如果找到一個 item,則返回 item 的第一次出現的位置。開始位置的索引為 0。
如果在數組中沒找到指定元素則返回 -1。
參數有兩個,其中第一個是(必填)需要查找的元素值,第二個是(可選)開始查找元素的位置
const arr = [1, 2, 3, 4]const index = arr.indexOf(3)console.log(index) // 2const index1 = arr.indexOf(2, 2)console.log(index1) // -1複制代碼
17 find(), findIndex()
findIndex() 方法返回數組中滿足提供的測試函數的第一個元素的索引。若沒有找到對應元素則返回-1。 找到返回滿足條件的第一個索引 find() 方法返回數組中滿足提供的測試函數的第一個元素的值。否則返回 [undefined] 找到返回第一個滿足條件的值
const arr = [1, 2, 3, 4] const found = arr.find((element) => element > 10) console.log(found)//undefined const found1 = arr.find((element) => element > 1) console.log(found1)//2 const found2 = arr.findIndex((element) => element > 1) console.log(found2)//-1 const found3 = arr.findIndex((element) => element > 1) console.log(found3)//1複制代碼
19 # includes()
方法确定數組是否在其條目中包含某個值,返回true或 false。
19 flat(n)
用于數組扁平化 不會改變原數組 ,返回值為扁平化後的新數組 ,參數n決定扁平化的深度,不傳默認為1
let arr = [1, 2, 3, [2, 3, [4, 5]]]console.log(arr.flat()) //[1, 2, 3, 2, 3, [4, 5]]console.log(arr.flat(2))// [1, 2, 3, 2, 3, 4, 5]console.log(arr)//[1, 2, 3, [2, 3, [4, 5]]]
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!