4.1.4 Array

数组对前端来说再常见不过了, 那么数组的那些方法到底熟悉不? 能否用普通函数实现一遍呢?

  1. 数组的map方法. 正常用法大家应该都知道, 比如:
var arr = [1, 2, 3];
var res = arr.map(function(item, index, arr){
    return item + index;
});
console.log(res); // [1, 3, 5]

接下来, 我就来用普通函数实现一下这个map函数, 叫xmap,挂在Arrayprototype上面

var arr = [1, 2, 3];
var res = arr.xmap(function(item, index, arr){
    console.log(this === arr)  // true
    return item + index;
}, arr);
console.log(res); // [1, 3, 5]

2.这里实现了find方法, 例子如下:

var arr = [
    {
        id: 1,
        name: 'vue'
    },
    {
        id: 2,
        name: 'react'
    },
    {
        id: 3,
        name: 'xui'
    },
];
var res = arr.xfind(function(item, index, arr){
    return item.id === 3;
});
console.log(res);  //[{id:3,name:'xui'}]

3.嗯哼, reduce方法也实现了, 要不看看?

var arr = [1, 2, 3, 4, 5];
var res = arr.xreduce(function(accum, next){
    return accum + next;
}, 6);
console.log(res); // 21

results matching ""

    No results matching ""