js数组方法一网打尽 (数组方法总结)

154 阅读3分钟

数组基础及常用的方法复习

创建数组的两种方式: 1.利用数组自变量

image.png

2.利用队new Array()

image.png

检测是否为数组 1.intanceof

image.png 2.Array.isArray()

image.png

数组常用的方法

1.reverse(反转数组)

image.png

2.添加和删除数组元素

  • 添加数组 push和unshif push是添加到最后一个元素后面,unshif是添加到第一个元素前面

image.png

  • 删除数组元素 pop和shift pop是删除到最后一个元素,shif是删除第一个元素

image.png

3.连接数组 concat()这个方法会先创建当前数组的一个副本,然后将接受到的参数添加到这个副本的末尾,最后返回数新构建的数组(且用)

 let colors1 = ['red','yellow', 'blue']
 let colors2 = colors1.concat('white', ['black', 'green'])
 console.log(colors1)//["red", "yellow", "blue"] 
 console.log(colors2)//["red", "yellow", "blue", "white", "black", "green"]

ES6...

let colors1 = ['red','yellow', 'blue']
 let colors2 = [... colors1'white', 'black', 'green') console.log(colors1)//["red", "yellow", "blue"] 
 console.log(colors2)//["red", "yellow", "blue", "white", "black", "green"]

4.截取数组 slice()截取数组方法 左开右闭区间 返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]
console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

splice()方法,由 其实位置 和 删除元素的个数 替换的元素,当传参数为空的时候,会返回空数组,会改变元素数组

let arr[1,45,6,343,23,2,]
arr.splice()//{}
arr.splice(-2)//arr[23,2],arr[1,45,6,343]
arr.splice(2)//arr[6,343,23,2],arr[1,45]
//第二位参数,传入不合法的值,结果都为空
arr.splice(0,-2)//{}  
arr.splice(0,0,['a','b'])//[['a','b'],1,45,6,343,23,2,]

5.索引查找方法 1.indexof()从左开始查找 2.lastIndexof()从右开始查找 3.includes() 4.find()返回第一个符合条件的值 5.findIndex()返回第一个符合条件的索引

let a[1,2,3,4,5,6]
a.indexOf(3)//2
a.lastIndexof(3)//2
a.includes(6) //true
a.find(function(n){
return n > 4
})  //5
a.findIndex(function(n){
return n > 4
})  //4

6.ES6新增方法 fill()填充方法 修改原数组 参数:填充值,替换起始位置,替换end Array.of() 单个的值 Arrayfrom() 来源是类数组

Array(3) //(3)[empty * 3]      3是数组长度
Array.of(3) //[3] 
Array.of(3,4,5) //[3,4,5]
Arry.from('123') //['1','2','3']

arr[1,23,4,5,6,]
arr.fill(6)//[6,6,6,6,6]
arr.fill(6,0,3)//[6,6,6,5,6]

数组的遍历方法 1.forEach() 遍历方法 传入参数 item循环当中的每一项,index当前所在对应的索引,arr当前数组本身;

  • 没有返回值
  • 第二个参数:this返回上级
  • 中间值不存在被并不会调用forEach方法
  • 有局限性,break,continue不可用
arr[1,2,3,4,5,6]
arr.forEach(item,index,arr) 
console.log(item,index,arr) // 1,0[1,2,3,4,5,6]
return 1; //undefined


let obj = {
    name : 'haoda',
    times:[1,2,3,4],
    print:function(){
        console.log(this) //obj
        
         //this.times.forEach(function(item){
         //    consloe.log(this) //obj 
         //},this)
           this.time.forEach(function(()=>{
               console.log(this) //obj
            },this)
}

image.png

2.map()映射方法

  • 可以返回一个数组, 这个数组可以和原数组是一个映射关系的数组
  • 第二个参数this可以改变指向
arr[1,2,3,4,5]

let arr1 = arr.map(item,index,arr){
    return item*2
}
console.log(arr1)//[2,4,6,8,10]

//应用场景
let arr = [
   { 'name' = 'zhangsan',
    'age '= 19,
    'sex' = 'male'
    },
    { 'name' = 'lisi',
    'age '= 29,
    'sex' = 'male'
    },
    { 'name' = 'wangwu',
    'age '= 31,
    'sex' = 'male'
    },
]
let newArr = user.map(function(item){
retuen arr.name
});

console.log(newArr)

filter():

  • 一定要返回一个布尔值,flase就过滤掉,true就保留
    let arr = [1,2,3,NAN,4,null,5,'',-1,0,false]
    let arr = arr.filter(function(item,index,arr)){
        return != undefined;
       }); //[1,2,3,4,5,-1,0]

some()

  • 只要一个成员通过就返回true every() -全部成员通过才返回true
let arr = [1,2,3,4,5]
let res1 = arr.some(function(item,index,arr){
    return itme % 2 === 0;  //true
})
       
let res2 = arr.every(function(item,index,arr){
    return item % 2 === 0;  //false
})       

reduce() 归纳函数,累加器 参数accumnlator 初始值 currentValue 当前值 currentIndex 当前值索引 array 当前数组

  • 必须要有返回值
let arr[1,7,8,3,6]
arr.reduce(function(accumnlator,currentValue,currentIndex,array){
    console.log(accumnlator,currentValue,currentIndex,array)//{} 1 0 [1,7,8,3,6]    underfined 7 1 [1,7,8,3,6]  underfined 8 2 [1,7,8,3,6]  underfined 3 3 [1,7,8,3,6]...
},{})


arr.reduce(function(pre,arr){
    let result = pre + arr
    return result   //0 + 1 +7 + 8 + 3 + 6 = 25
},0)