map的原生写法

天明   ·   发表于 2021-6-6   ·   前端

map

特点:
map处理return出来的值,把值放在一个新的数组,默认返回undefined。
浅拷贝,会改变原数组。
浅度map实现


Array.prototype.myMap = function(func) {    var len = this.length,        newArr = [];    for(var i = 0; i < len; i++) {        newArr.push(func(this[i], i));    }    return newArr;}


深度map实现


// 深度克隆function deepClone(target, option) {    if(option !== null) {        for(var  prop in option) {            var src = target[prop],                copy = option[prop];            if(typeof(copy) === 'object') {                if(Object.prototype.toString(copy) === '[object Array]') {                    src = [];                }else {                    src = {};                }                target[prop] = deepClone(src, copy);            }else {                target[prop] = copy;            }        }    }       return target;}Array.prototype.myMap = function(func) {    var len = this.length,        newArr = [];    for(var i = 0; i < len; i++) {        if(this[i] && Object.prototype.toString.call(this[i]) === '[object Object]') {            var newObj = {};            deepClone(newObj, this[i]);            newArr.push(func(newObj, i));        }else if(this[i] && Object.prototype.toString.call(this[i]) === '[object Array]') {            var Arr = [];            deepClone(newArr, this[i]);            newArr.push(func(Arr, i));        }else {            newArr.push(func(this[i], i));        }    }    return newArr;}


0 条回复   |  直到 2021-6-6 | 1172 次浏览
登录后才可发表内容