阅读 180

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]]]


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐