js定義有返回值的方法:js函數返回值類型

js 常用數組數組方法總結

1 拼接截取轉換

1 concat()

作用:方法用於連接兩個或多個數組
改變原數組:否
返回值:拼接後的新數組
參數:可以是具體值,也可以是一個數組

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-3
console.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')) // 7
console.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 = 0
const 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) // 2
const 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]]]

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/224335.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:37
下一篇 2024-12-09 14:37

相關推薦

發表回復

登錄後才能評論