数组方法

80 阅读2分钟
Array.of() 新增方法主要与new Array区别 单个参数不会创建空元素
let arr1 = Array.of(2);   // [2]
let arr2 = Array.of(1,2,4,3);  // [1,2,4,3]
Array.isArray()方法主要判断一个变量是否为数组
let num = 100;
let arr = [1,2,3,40];
let str = 'string';
console.log(Array.isArray(num),Array.isArray(arr),Array.isArray(str));   // false true false
Array.from() 方法 用于类数组转化为数组 类数组就是有length属性或者可迭代对象
let str = 'string';
console.log(str.length);  // 6
console.log(Array.from(str));   // [ 's', 't', 'r', 'i', 'n', 'g' ]

//  有length属性的对象   并且下标为数值或者字符串数值  可以通过该方法转化为数组
let person = {
    0 : 'name',
    1 : 'age',
    length: 2
};
console.log(Array.from(person));   // [ 'name', 'age' ]

 //   类数组  比如说函数中的arguments
 function f(){
     return Array.from(arguments);
 }
 console.log(f(1,2,5,99));   // [ 1, 2, 5, 99 ]

 // set类型转数组  
 let set = new Set(str);
 console.log(Array.from(set));   // [ 's', 't', 'r', 'i', 'n', 'g' ]
 console.log([...set]);  // 用扩展即可把类数组转为数组  [ 's', 't', 'r', 'i', 'n', 'g' ]

 //接头函数转为数组
 console.log( Array.from([...str],i => i+'0'));  // [ 's0', 't0', 'r0', 'i0', 'n0', 'g0' ]

 let arr = Array.from({length:6},(v,i) => i);
 console.log(arr);   // [ 0, 1, 2, 3, 4, 5 ]

 console.log(Array.from({length:3}));  //  [ undefined, undefined, undefined ]


 //  数组去重  利用set元素不能重复原理

 let NumArr = [1,2,3,4,3,4,2,4,5,4,'3'];

 function remove(arr){
    // return [...new Set(arr)];  // 利用es6中的...扩展属性
    return Array.from(new Set(arr));  // Array.from  方法
 }

 console.log(remove(NumArr));  //  [ 1, 2, 3, 4, 5, '3' ]


 //   数组去重合拼 
 let StrArr = ['1','s','3'];
 let ObjArr = [{name:'xue'}];

 function combine(){
     let arr = [].concat.apply([],arguments);
     return [...new Set(arr)];
 }
 
 console.log(combine(NumArr,StrArr,ObjArr));   //  [ 1, 2, 3, 4, 5, '3', '1', 's', { name: 'xue' } ]
数组原型方法

 //  slice 对一个数组进行切割  对一个数组进行浅拷贝

 console.log(NumArr.slice(1,4));   // [ 2, 3, 4 ]

 //  splice  对一个数组进行切割  删除 或 替换   或  新增 来改变数组    会直接对原数组改造   返回新数组

 let  arr11 = [1,2,33,4,11];
 console.log(arr11.splice(1,1,100));   // [ 2 ]
 console.log(arr11.splice(1,0,600));   // []
 console.log(arr11.splice(1,1));  // [ 600 ]
 
 console.log(arr11);   //  [ 1, 100, 33, 4, 11 ]   [ 1, 600, 100, 33, 4, 11 ]  [ 1, 100, 33, 4, 11 ]


 //  arr11  [ 1, 100, 33, 4, 11 ]
 //  indexof  查找数组元素  找到返回元素索引   没有找到就返回-1
 //  lastindexof   查询数组元素   右往左查找   找到返回索引  没有返回 -1
 console.log(arr11.indexOf(100));   // 1
 console.log(arr11.indexOf(99));    // -1
 console.log(arr11.indexOf(100,2)); // -1
 console.log(arr11.lastIndexOf(1)); // 0   从右往左查找
 

 // includes()  返回布尔值   查询到返回true  查询不到返回false
 console.log(arr11.includes(100));   // true
 

// find  查询数组中经过回调函数条件的第一个元素   没有返回undefined
console.log(arr11.find((value)=>value>10));    // 1000
console.log(arr11.find(value => value >1000)); // undefined

//findeIndex   返回满足测试函数条件的第一个元素的索引   没有就返回 -1
console.log(arr11.findIndex(value => value >2));  // 1
console.log(arr11.findIndex(value => value>100)); // -1

//  reduce
console.log(arr11.reduce((sum,cur)=>{
    sum+=cur;
    return sum;
},0));   // 149

//reduce实现一个数组中出现各个元素出现的次数
let arrName = ['xw','xe','wq','xw','qw','wq','xw'];
let names = arrName.reduce((names,name) =>{
    name in names ? names[name]++ : names[name] = 1;
    return names;
},{});
console.log(names);   // { xw: 3, xe: 1, wq: 2, qw: 1 }