underscorejs之_.map(list, iteratee, [context])

本文介绍了如何使用Underscore.js库中的_.map方法对不同类型的集合进行操作,并返回新的数组。详细展示了对数组、对象、字符串及arguments的操作示例,同时讲解了iteratee函数的参数传递、return值的重要性及context的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语法:

_.map(list, iteratee, [context])

说明:

对集合的每个成员依次进行某种操作,将返回的值依次存入一个新的数组。接收3个参数。list可理解为数据源iteratee迭代器可理解为回调方法;context执行上下文。

  • list可以操作数组,对象,字符串和arguments
  • iteratee 会传第三个参数(element, index, list)或(value, key, list)
  • iteratee里面需要返回值。
  • context可以改变iteratee内部的this

代码示例:

示例一:map对数组、对象、字符串和arguments进行操作并返回数组。
var result;

//操作数组
result =  _.map([1, 2, 3], function (element, index, list) {
    return element + 1;
});
console.log(result) //=> [2, 3, 4]

//操作对象
result = _.map({one: '一', two: '二', three: '三'}, function(value, key, list){
    return value + 1;
});
console.log(result) //=> ["一1", "二1", "三1"]

//操作字符串
result = _.map('123', function(element, index, list){
    return element + 1;
});
console.log(result) //=> ["11", "21", "31"]

//操作arguments
function abc(){
    result = _.map(arguments, function(element, index, list){
       return element + 1;
    });
    console.log(result); //=> [2, 3, 4]
}
abc(1, 2, 3);
示例二:iteratee传递的参数
var result;

//数组的情况
result = _.map([1, 2, 3], function (element, index, list) {
    console.log(element, index, list);
    //=> 1 0 [1, 2, 3]
    //=> 2 1 [1, 2, 3]
    //=> 3 2 [1, 2, 3]
});

//对象的情况
result = _.map({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: "三"}
});
示例三:iteratee内
示例三:iteratee内部需要有return值
var arr1 = _.map([1, 2, 3], function (element, index, list) {
    element + 1;
});

var arr2 = _.map([1, 2, 3], function (element, index, list) {
    return element + 1;
});
console.log(arr1); //=> [undefined, undefined, undefined]
console.log(arr2); //=> [2, 3, 4]
示例四:context可以改变iteratee内部的this
var result = _.map([1, 2, 3], function (element, index, list) {
    return element + this.no; //this为{no : 10}
}, {no : 10});

console.log(result);//=> [11, 12, 13]
示例五:map方法执行后,list不变,返回新数组。
var list = [1, 2, 3];

var result = _.map(list,  function(element, index, list){
    return element + 1;
});

console.log(list); //=> [1, 2, 3]
console.log(result); //=> [2, 3, 4]

_.collect的功能和_.map是一样的

var result = _.collect([1, 2, 3],  function(element, index, list){
    return element + 1;
});
console.log(result); //=> [2, 3, 4]

操作非集合,返回空数据

var arr1 = _.map(null, function (element, index, list) {
    console.log(element); //不执行
});

var arr2 = _.map(undefined, function (element, index, list) {
    console.log(element); //不执行
});

var arr3 = _.map(123, function (element, index, list) {
    console.log(element); //不执行
});

var arr4 = _.map(new Date(), function (element, index, list) {
    console.log(element); //不执行
});
console.log(arr1); //=> []
console.log(arr2); //=> []
console.log(arr3); //=> []
console.log(arr4); //=> []

 iteratee还可以是全局的方法

var result = _.map([1, -2, -3], Math.abs);
console.log(result); //=> [1, 2, 3]

 iteratee里面用console.log需要bind(坑)

var result = _.map([1, -2, -3], console.log.bind(console));
//=> 1 0 [1, -2, -3]
//=> -2 1 [1, -2, -3]
//=> -3 2 [1, -2, -3]

 

转载于:https://www.cnblogs.com/rechel/p/7615156.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值