6>数组方法

119 阅读4分钟

Array.from()

将一个类数组结构转为数组实例

let arr = 'hello'
let arr1 = Array.from(arr)
console.log(arr1)     // ['h', 'e', 'l', 'l', 'o']

此时还可以用解构
console.log([...arr]);    // ['h', 'e', 'l', 'l', 'o']

Array.of()

将一组参数转为数组

let arr = Array.of(1, 2, 3, 4, 5)
console.log(arr);    // [1, 2, 3, 4, 5]

Array.isArray()

检测是否是数组

let arr = [1, 2, 3, 4, 5]
console.log(Array.isArray(arr));    // true

Array原型上暴露的三个方法返回迭代器

keys() values() entries() 这些方法返回迭代器,可以用Array.from()直接返回数组实例

let arr = [1, 2, 3, 4, 5]
console.log(Array.from(arr.keys()));    // [0, 1, 2, 3, 4]    返回索引
console.log(Array.from(arr.values()));    // [1, 2, 3, 4, 5]   返回值
console.log(Array.from(arr.entries()));    // [[0, 1], [1, 2], [2, 3], [3, 4], [5, 6]]   返回键值对

fill()

填充数组

let arr = new Array(5).fill('')
console.log(arr);    // ['', '', '', '', '']

let arr = ['', '', '', '', '']
console.log(arr.fill(2, 2));    // ['', '', 2, 2, 2]  用2填充大于等于2的索引

console.log(arr.fill(2, 2, 4));    // ['', '', 2, 2, '']  用2填充大于等于2小于4的索引
console.log(arr.fill(0));    // [0, 0, 0, 0, 0]  重置为全0

join()

将数组转为字符串

let arr = [0, 1, 2, 3, 4]
console.log(arr.join('-'));  // 0-1-2-3-4

push() 和 pop()

栈是一种后进先出的结构,也就是新添加的项也会被先删除,这个操作在栈顶发生 数组的push()和pop()方法实现了类似栈的行为

let arr = [0, 1, 2, 3, 4]
let a = arr.push('hello')
console.log(a);      // 6    这里不同的是push()返回新数组长度
console.log(arr);    // [0, 1, 2, 3, 4, 'hello']  
let b = arr.pop()    
console.log(b);      // 'hello'   pop()返回的是删除的项
console.log(arr);    // [0, 1, 2, 3, 4]

unshift() 和 shift()

队列模式
shift()会删除数组的第一项,和push()一起使用,可当成队列使用 unshift()会从数组开头添加值 ,和pop()一起使用,可当成反队列使用

let arr = [0, 1, 2, 3, 4]
let a = arr.unshift('hello')
console.log(a);      // 6    这里一样返回数组的长度
console.log(arr);    // ['hello', 0, 1, 2, 3, 4]  
let b = arr.shift()    
console.log(b);      // 'hello'   shift()返回的是删除的第一项
console.log(arr);    // [0, 1, 2, 3, 4]

reverse() 和 sort(val, val2)

数组排序,这里操作的是数组对象的索引

let arr = [0, 1, 2, 3, 4]
console.log(arr.reverse());  //  [4, 3, 2, 1, 0]  反转数组

let arr = [0, 18, 2, 35, 14]
let arr2 =  arr.sort((a, b)=>{
    return a - b    // 升序
})
console.log(arr2);    // [0, 2, 14, 18, 35]   

let arr3 =  arr.sort((a, b)=>{
    return b - a   // 降序
})
console.log(arr3);    // [35, 18, 14, 2, 0]
console.log(arr);    // [35, 18, 14, 2, 0]   会影响原始数组

concat() ...操作符 // 数组合并

concat()会返回一个新数组实例,用来合并数组,或者合并参数,此处为深拷贝,不会影响原数组 ...操作符则是浅拷贝

let arr = ['hello', 'wold', 'haha']
let arr3 = arr.concat(['no', 'good'])
let arr4 = arr.concat('very')

console.log(arr3);    //  ['hello', 'wold', 'haha', 'no', 'good']  打平数组,可以设置symbol符号是否打平数组
console.log(arr4);    //  ['hello', 'wold', 'haha', 'very']
console.log(arr);     //  ['hello', 'wold', 'haha']

let arr5 = [...arr, ...['haha', 'heihei']]  //  ['hello', 'wold', 'haha', 'haha', 'heihei']

slice(start, end)

数组截取也不会影响原数组,参数为截取起始位置,但是不包含结束位置

let arr = ['hello', 'wold', 'haha']
let arr3 = arr.slice(1)
let arr4 = arr.concat('very')

console.log(arr.slice(1));    //  [ 'wold', 'haha']  
console.log(arr.slice(1,2));    //  ['haha']  

splice()

数组最强大的方法,可以删除插入替换

let arr = ['hello', 'wold', 'haha', 'hello', 'wold', 'haha']

//删除 splice(idx,num)  参数为开始的下标位置,和要删除的个数
let arr2 = arr.splice(2, 3)
console.log(arr);    //  ['hello', 'wold', 'haha'] 

//插入 splice(idx,num,val,val2) 参数为开始的下标位置,和要删除的个数, 和插入的值 
如果第二个参数为0,则为插入,如果>0,则先删除相应个数再插入,后面可以继续添加多个参数,均为要插入的值 
let arr3 = arr.splice(2, 0, 'red', 'blue')
console.log(arr);    
//  ['hello', 'wold', 'red', 'blue', 'haha', 'hello', 'wold', 'haha'] 

// 替换,如果第二个参数删除和添加的个数一样为替换
let arr4 =  arr.splice(2, 2, 'red', 'blue')
console.log(arr);    
//  ['hello', 'wold', 'red', 'blue', 'wold', 'haha']

console.log(arr.splice(2, 0, 'red', 'blue'));    
// []  注意,这里返回的是删除的值,如果没有删除则返回空数组

includes()

判断元素是否存在数组中,返回布尔值 ,也可以用来判断字符串

let arr = ['hello', 'wold', 'haha', 'hello', 'wold', 'haha']
console.log(arr.includes('wold'))  // true

let str = 'hello world'
console.log(str.includes('world'))  // true

find() 和 findIndex()

这个函数都使用了断言函数, find() 返回第一个匹配到的元素 findIndex() 返回第一个匹配到的元素的下标

let arr = ['hello', 'wold', 'haha', 'hello', 'wold', 'haha']
let arr2 = arr.find((item, index)=> item == 'haha')
console.log(arr2)  // 'haha'

let arr3 = arr.findIndex((item, index)=> item == 'haha')
console.log(arr3)  // 2

比较两个数组对象,取出不同的值

var array1 = [ {"Num": "A " },{"Num": "B" }]; 
var array2 = [ {"Num": "A ","Name": "t1 " }, {"Num": "B","Name": "t2"}, {"Num": "C " ,"Name": "t3 "}]; 
var result = []; 
for(var i = 0; i < array2.length; i++){ 
    var obj = array2[i]; 
    var num = obj.Num; 
    var isExist = false; 
    for(var j = 0; j < array1.length; j++){ 
        var aj = array1[j]; 
        var n = aj.Num; 
        if(n == num){ 
            isExist = true; 
            break; 
        } 
    } 
    if(!isExist){ 
        result.push(obj); 
    }
} 
console.log(result);

every(), filter(), map(), forEach(), some()

every()和some()都是返回符合条件的布尔值 map()返回处理后的数组 filter()返回符合条件的数组 forEach()相当于for循环,进行处理操作

let  arr = [
    {
        num:1
    },
    {
        num:3
    },
    {
        num:1
    },
]
let bol1 = arr.every((item,index)=> item.num < 5)
console.log(bol1);   // true

let bol2 = arr.some((item ,index)=> item.num == 1)
console.log(bol2);   // true

let arr2 = arr.map((item ,index)=> item = {num:item.num + 10})
console.log(arr2);   // [{num:2},{num:4},{num:2}]

let arr3 = arr.filter((item ,index)=> item.num == 1)
console.log(arr3);  // [{num:1},{num:1}]

reduce() 和reduceRight()

归并方法 reduce() 从左往右遍历 和reduceRight() 从右往左遍历

将数组按固定个数分成几份

pageArr(arr, page){
    let newArr = []
    for(let i = 0;i <arr.length;i = page+i){
        newArr.push(arr.slice(i , i+page))
    }
    return newArr
}