手写map, filter函数

function map(arr, fn) {
    let newArr = [];

    for (let i = 0; i < arr.length; i++) {
        newArr[i] = fn(arr[i], i, arr)
    }

    return newArr;
}

function filter(arr, fn) {
    let newArr = [];

    for (let i = 0; i < arr.length; i++) {
        if (fn(arr[i], i, arr)) {
            newArr.push(arr[i]);
        }
    }

    return newArr;
}
var arr=[1,2,3,4,5,6]
console.log(map(arr,(item)=>item+1))
console.log(filter(arr,(item)=>item>3))

  来源:https://www.cnblogs.com/superlizhao/p/11477511.html

posted @ 2020-02-11 00:02  益码凭川  阅读(253)  评论(0编辑  收藏  举报