手写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))

 

posted @ 2019-09-06 18:46  哥哦狗子  阅读(1303)  评论(0编辑  收藏  举报