JavaScript中数组的常用方法---复习

数组的常用方法

  1. join() 将所有数组元素结合为一个字符串
let arr = ['l', 'u', 'y', 'a', 'o'];
let a = arr.join('');
console.log(a); //luyao
  1. push() 向数组末尾添加元素,返回值为数组的长度,也可一次性增加多个元素
let arr = ['l', 'u', 'y', 'a', 'o'];
let a = arr.push('W','hello');
console.log(a); //7
console.log(arr); //[ 'l', 'u', 'y', 'a', 'o', 'W' ,'hello']
  1. unshift() 在数组头部添加元素,返回值为数组的长度
let arr = ['l', 'u', 'y', 'a', 'o'];
let a = arr.unshift('W');
console.log(a); //6
console.log(arr); //[ 'W', 'l', 'u', 'y', 'a', 'o' ]
  1. pop() 删除数组末尾的数据,一次只能删一个,返回值为删除的元素
let arr = ['l', 'u', 'y', 'a', 'o'];
let a = arr.pop('o');
console.log(a); //o
console.log(arr); //['l', 'u', 'y', 'a']
  1. shift() 删除首个数组元素,并把所有其他元素“位移”到更低的索引
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.shift();
console.log(a); //W
console.log(arr); //['l', 'u', 'y', 'a', 'o' ]
  1. splice(index,howmany,item1,…itemx)
    作用:
    ① 删除元素:两个参数,从index开始删除howmany个元素,返回值为数组
    ② 增加元素:>三个参数,从index开始howmany=0的条件下,添加元素
    ③ 替换元素:>三个参数,从index开始删除howmany个元素的条件下,添加howmany个元素
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
//删除元素
let a = arr.splice(0, 2);
console.log(a); //[ 'W', 'l' ]
console.log(arr); //[ 'u', 'y', 'a', 'o' ]
//增加元素
let b = arr.splice(1, 0, 'hello')
console.log(b); //[]
console.log(arr); //[ 'u', 'hello', 'y', 'a', 'o' ]
//替换元素
let c = arr.splice(3, 1, 'hello');
console.log(c); //['a']
console.log(arr); //[ 'u', 'hello', 'y', 'hello', 'o' ]
  1. concat() 数组合并,支持多个参数,可合并多个数组
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.concat([0, 2], [1, 2], 1);
console.log(a);//['W', 'l', 'u', 'y', 'a','o', 0, 2, 1, 2, 1]
let b = arr.concat(2, 1);
console.log(b);//['W', 'l', 'u', 'y','a', 'o', 2, 1]
  1. slice(start,end) 用数组的某个片段切出新数组, 开始索引到结束数组索引
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.slice(1);
console.log(a); //[ 'l', 'u', 'y', 'a', 'o' ]
console.log(arr); //['W', 'l', 'u', 'y', 'a', 'o']
let b = arr.slice(1, 2);
console.log(b); //['l']
  1. toString() 数组转字符串
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.toString()
console.log(a); //W,l,u,y,a,o
  1. reverse() 倒序;将数组中元素的顺序颠倒
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.reverse()
console.log(a); //[ 'o', 'a', 'y', 'u', 'l', 'W' ]
  1. sort() 排序;数组排序,默认升序,按照ASCII码排序;支持自定义排序规则,使用函数实现,作为sort参数
let arr = ['W', 'l', 'u', 'y', 'a', 'o'];
let a = arr.sort()
console.log(a); //[ 'W', 'a', 'l', 'o', 'u', 'y' ]
arr.sort(function(a, b) { return a - b }); //升序
arr.sort(function(a, b) { return b - a }) //降序
arr.sort(function(a, b) { return 0.5 - Math.random() }); //随机排序
  1. indexOf() 判断元素在数组中的位置
let arr = [1, 3, 4, 9, 6, 5, 2, 1];
let a = arr.indexOf(1)
console.log(a);//0(元素第一次出现的索引)
  1. lastIndexOf(item, start) 判断元素在数组中的位置
    item:必需。要检索的项目。
    start:可选。从哪里开始搜索。负值将从结尾开始的给定位置开始,并搜索到开头。
let arr = [1, 3, 4, 9, 6, 5, 2, 1];
let a = arr.lastIndexOf(1)
console.log(a); //7(元素最后一次b出现的索引)
let b = arr.lastIndexOf(1, 6)
console.log(b); //0(从索引为6的开始元素最后一次出现的索引)
  1. forEach() 遍历,接受一个函数作为参数(有三个参数),遍历数组,无返回值
arr.forEach(function(item,index,arr){console.log(item,index,arr)})
  1. every() 有返回值,布尔类型,若数组中所有满足规则,返回true,否则返回false
let arr = [1,2,68,2]
let a = arr.every(function(item) { return item < 5 })
console.log(a); //false
  1. some() 有返回值,布尔类型,若数组中有一个满足规则,返回true,都不满足返回false
let arr = [1, 3, 4, 9, 6, 5, 2, 1];
let a = arr.some(function(item) { return item < 5 })
console.log(a); //true
  1. filter() 有返回值,返回值为新数组,将所有满足条件的筛选出来,形成一个新的数组
var numbers = [45, 4, 9, 16, 25];
var a = numbers.filter(myFunction);
function myFunction(value, index, array) {
    return value > 18;
}
console.log(a); //[ 45, 25 ]
  1. map() 有返回值,新数组,将所有按规律生成的元素放入新数组中返回
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
function myFunction(value, index, array) {
  return value * 2;
}
console.log(numbers2); //[ 90, 8, 18, 32, 50 ]
  1. find() 找出第一个符合该函数参数条件的数组成员,函数的参数是一个回调函数。是第一个符合,不是所有,也就是一旦找到一个符合的就return
    如果没有符合的就返回undefind
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
let res1 = arr1.find((value, index, arr1) => {
    return value > 4
})
let res2 = arr1.find((value, index, arr1) => {
    return value > 14
})
console.log(res1) //5
console.log(res2) //undefined
  1. findIndex() 与前面的find类似,但是返回的不是数组成员而是成员的索引,没有符合的返回-1,与find一样可以发现NaN这是在indexOf上的改进
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let res3 = arr1.findIndex((value, index, arr1) => {
    return value > 4
})

let res4 = arr1.findIndex((value, index, arr1) => {
    return value > 14
})
console.log(res3) //4
console.log(res4) //-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值