# 数组操作

# 数组去重

  • 利用 set 去重
let arr = [1,2,3,1,5]
console.log(Array.from(new Set(arr)))
  • 利用下标
const arr = [1,2,3,1,5]
function unique2(arr){
    const hash=[];
    for (let i = 0; i < arr.length; i++) {
        if(arr.indexOf(arr[i])==i){
            hash.push(arr[i]);
        }
    }
    return hash;
}
console.log(unique2(arr));

# 找出数组中的最大值

function getMax(arr){
  if(arr.length ===0) return
  if(arr.length ===1) return arr[0]
  return Math.max(...arr)
}

# Math.max的实现


function max() {
    const args= Array.prototype.slice.call(arguments)
    let max = 0;
    args.forEach((item)=>{

        if(item>max){

            max = item
        }
    })

    return max
}
console.log(max(20,40,30))

# 数组扁平化处理

  • 通过递归的方式
/* ES6 */
const flatten = (arr) => {
  let result = [];
  arr.forEach((item, i, arr) => {
    if (Array.isArray(item)) {
      result = result.concat(flatten(item));
    } else {
      result.push(arr[i])
    }
  })
  return result;
};

const arr = [1, [2, [3, 4]]];
console.log(flatten(arr));
  • 通过 toString 方法
[1, [2, 3, [4]]].toString() // "1,2,3,4"
/* ES6 */
const flatten = (arr) => arr.toString().split(',').map((item) => +item);

const arr = [1, [2, [3, 4]]];
console.log(flatten(arr));
  • 通过 es6的flat方法
console.log([1,2,3[3,4]].flat(2))//参数代表展开的层级

# 不使用循环,创建一个长度 100 的数组

  • 方法 1 Array.keys()

    Array.from(new Array(100).keys())

  • 方法 2 Array.from()

    var arr = Array.from({length:100}, (v,k) => k); console.log(arr);

# slice(0)妙用

  • slice(0),如果是多层数组是浅拷贝,一层数组是深拷贝。
  • splice 会改变原数组,slice 不会改变原数组。
  • splice(0)会把以前的数据清空,并使用一个新数组。
//例题1
let a = [1,2,3,4]
let b = a.splice(0)
console.log(a)//[]
console.log(b)//[1,2,3,4]
//例题2
//单层数组深拷贝
let a = [1,2,3]
let b = a.slice(0);
a[0]=99
console.log(b);//[1,2,3]

//例题3
//多层数组浅拷贝
let a = [1,2,3]
let b = [[...a]].slice(0);
console.log(b);//[ [ 1, 2, 3 ] ]
b[0][0] = 99;
console.log(b)// [ [ 99, 2, 3 ] ]

# reduce方法

作用

  • 对子元素,纯数字类型或者子元素为对象进行累加操作
  • 二维数组转化为一维数组
  • 根据属性对object分类
* temp 累计存储的变量,temp是个临时状态,reduce每次执行会刷新这个状态,作为下一次temp的值。
* item 当前项
* index 索引
* data 源数组
*/
return [0,1,2,3,4].reduce(function (temp,item,index,data)
    return temp + item;                            
},0)

let a  = [{num:2},{num:4},{num:10}].reduce((pre,cur)=>cur.num + pre,0)

//二维转化为一维
let arr = [[1,2,3],[45,4]].reduce((pre,cur)=>pre.concat(cur),[])

//统计每个子元素出现的次数
['a','b','a','c'].reduce((target,cur)=>{
    if(cur in target){ target[cur] ++ }
    else{target[cur] = 1}
    return target;
},{})

const arr = [
    {name:'wang',age:20},
    {name:'king',age:10},
    {name:'li',age:20},
]

arr.reduce((target,cur)=>{
    let key = cur['age'];
    if(!target[key]){
        target[key] = []
    }
     target[key].push(cur)
    return target;
},{})

//做聚合
const arr = [{id: 1,type: 'A',total: 3}, {id: 2,type: 'B',total: 5}, {id: 3,type: 'E',total: 7}]
//做累加
arr.reduce((sum, { total }) => {
  return sum + total;
}, 0)

//聚合为字符串
arr.reduce((str, { id, type }) => {
  return str + `id:${id},type:${type};`;
}, '')

//聚合为对象
arr.reduce((res, { id, type, total }) => {
  res[id] = {
    type,
    total
  };
  return res;
}, {})

  • 给第二个参数初始化为200
console.log(fn8([1,2,3,4,5]))
function fn8(arr){
    return   arr.reduce(function (temp,item,index,data){
        console.log('----temp',index,temp)
        return temp + item;
    },200)
}
//打印结果
----temp 0 200
----temp 1 201
----temp 2 203
----temp 3 206
----temp 4 210
215
  • 给第二个参数初始化为0
console.log(fn8([1,2,3,4,5]))
function fn8(arr){
    return   arr.reduce(function (temp,item,index,data){
        console.log('----temp',index,temp)
        return temp + item;
    },0)
}
//打印结果
----temp 0 0
----temp 1 1
----temp 2 3
----temp 3 6
----temp 4 10
15
  • 不传第二个参数 当不传第二个参数,reduce函数从第二项开始循环,temp初始值为数组第一个元素
console.log(fn8([1,2,3,4,5]))
function fn8(arr){
    return   arr.reduce(function (temp,item,index,data){
        console.log('----temp',index,temp)
        return temp + item;
    })
}
----temp 1 1
----temp 2 3
----temp 3 6
----temp 4 10
15

# forEach

  • 1不能使用break。
  • 2item代表当前项,item是原数组的拷贝,在forEach或者map中操作修改item是不能改变原数组的。
//不可以改变原数组
console.log(fn8([1,2,3,4,5]))
function fn8(arr,i){
    arr.forEach((item,i,data)=>{
        item = item+2
    })
    console.log(arr)
}

//可以改变原数组
console.log(fn8([1,2,3,4,5]))
function fn8(arr,i){
    arr.forEach((item,i,data)=>{
        data[i] = item+2
    })
    console.log(arr)
}

# 实现一个Map函数

Array.prototype.myMap = function (fn,args){
    let self = this;
    let target = []
    console.log('---self',self)
    console.log('---args',args)
    for (let i =0;i<self.length;i++){
        target[i] = fn.call(args,self[i],i,self)
    }
    return target;
}
let a = [1,3,5];
console.log(a.myMap((item,i,data)=>{
    return item +1;
}))