5.4 mapObject

5.4.1 语法:

_.mapObject(list, predicate, [context])

5.4.2 说明:

对list集合的每个成员依次进行匹配(根据predicate迭代函数检测),返回一个对象

  • list可以为数组,对象,arguments和函数
  • predicate会传第三个参数value, key, list(参数名可自定义)
  • predicate函数需要返回值
  • context可以改变predicate函数内部的this

5.4.3 代码示例:

示例一:mapObject对数组,对象进行操作并返回一个对象

var result;

// 操作数组
result = _.mapObject([1, 2, 3], function (value) {
    return value + 1;
});
console.log(result) //=> {0: 2, 1: 3, 2: 4}

// 操作对象
result = _.mapObject({ one: '一', two: '二', three: '三' }, function (value) {
    return value + '0';
});
console.log(result) //=> {one: "一0", two: "二0", three: "三0"}

// 操作复杂的对象
var obj = {
    levelA: {},
    levelB: '一',
    levelC: 1
}
result = _.mapObject(obj, function (value) {
    if (typeof value === 'number') {
        value += 1;
    }
    return value;
});
console.log(result) //=> {levelA: {}, levelB: "一", levelC: 2}

//操作arguments
function abc() {
    result = _.mapObject(arguments, function (value) {
        return value + 1;
    });
    console.log(result); //=> {0: "一1", 1: "二1", 2: "三1"}
}
abc('一', '二', '三');

// 函数
function Stor() {
    this.one = 1;
}
Stor.prototype.two = 2; //通过给Stor对象的prototype属性给Stor对象添加一个two属性
result = _.mapObject(new Stor, function (value) {
    return value + 1;
});
console.log(result) //=> {one: 2}

示例二:predicate函数传递的参数(函数内部需要return返回值)

var result;

//数组的情况
result = _.mapObject([1, 2, 3], function (value, key, list) {
    console.log(value, key, list);
    //=> 1 0 [1, 2, 3]
    //=> 2 1 [1, 2, 3]
    //=> 3 2 [1, 2, 3]
    //return value;
});
console.log(result) //=> {0: undefined, 1: undefined, 2: undefined}

//对象的情况
result = _.mapObject({one: '一', two: '二', three: '三'}, function(value, key, list){
    console.log(value, key, list);
    //=> 一 one Object {one: "一", two: "二", three: "三"}
    //=> 二 two Object {one: "一", two: "二", three: "三"}
    //=> 三 three Object {one: "一", two: "二", three: "三"}
    //return value;
});
console.log(result) //=> {one: undefined, two: undefined, three: undefined}

示例三:context可以改变predicate内部的this

var result;

// 数组的情况
result = _.mapObject([1, 2, 3], function (value, key, list) {
    console.log(this); //=> [1, 2, 3] this是数组
}, [1, 2, 3]);

// 对象的情况
result = _.mapObject([1, 2, 3], function (value, key, list) {
    console.log(this); //=> Object {no: 10} this是对象
}, { "no": 10 });

// 字符串的情况
result = _.mapObject([1, 2, 3], function (value, key, list) {
    console.log(this); //=> String {0: "1", 1: "2", 2: "3", length: 3, [[PrimitiveValue]]: "123"} this是将字符串拆分后的对象
}, "123");

5.4.4 特殊情况

示例一:list的特殊情况

//例如:null,undefined,0,true,this等;
var result = _.mapObject(null, function (value, key, list) {
    return true;
});
console.log(result) //=> {}

示例二:predicate函数的this为window全局对象的情况

// 例如:null,undefined,window,this等
var result = _.mapObject([1, 2, 3], function (value, key, list) {
    console.log(this); //=> this是window全局对象
}, null);

示例三:predicate参数为一个字符的时候

var result = _.mapObject({ x: 1, y: 2 }, 'x');
console.log(result) //=> {x: undefined, y: undefined}