为什么javascript map函数返回undefined?

时间:2013-04-16 12:26:59

标签: javascript

我的代码

 var arr = ['a','b',1];
 var results = arr.map(function(item){
                if(typeof item ==='string'){return item;}  
               });

这给出了以下结果

["a","b",undefined]

我不希望在结果数组中使用undefined。我怎么能这样做?

9 个答案:

答案 0 :(得分:135)

如果该项不是字符串,则不返回任何内容。在这种情况下,函数返回undefined,您在结果中看到的内容。

map函数用于将一个值映射到另一个值,但看起来你实际上想要过滤数组,而map函数不适合。

您真正想要的是filter功能。它需要一个函数,根据您是否需要结果数组中的项目返回true或false。

var arr = ['a','b',1];
var results = arr.filter(function(item){
    return typeof item ==='string';  
});

答案 1 :(得分:13)

由于ES6 filter支持尖箭头符号(如LINQ):

所以它可以归结为跟随单行。

['a','b',1].filter(item => typeof item ==='string');

答案 2 :(得分:12)

过滤器适用于未修改项目的特定情况。 但在许多情况下,当您使用地图时,您希望对传递的项目进行一些修改。

如果这是你的意图,你可以使用reduce

var arr = ['a','b',1];
var results = arr.reduce((results, item) => {
    if (typeof item === 'string') results.push(modify(item)) // modify is a fictitious function that would apply some change to the items in the array
    return results
}, [])

答案 3 :(得分:8)

如果当前元素为string,则只返回一个值。也许分配一个空字符串就足够了:

var arr = ['a','b',1];
var results = arr.map(function(item){
    return (typeof item ==='string') ? item : '';  
});

当然,如果要过滤任何非字符串元素,则不应使用map()。相反,您应该考虑使用filter()函数。

答案 4 :(得分:4)

var arr = ['a','b',1];
 var results = arr.filter(function(item){
                if(typeof item ==='string'){return item;}  
               });

答案 5 :(得分:4)

我的解决方案是在地图后使用过滤器。

这应该支持每种JS数据类型。

示例:

    const noUndefinedList = someList
          .map(// mapping condition)
          .filter(notUndefined => notUndefined); // by doing this, 
                      //you can ensure what's returned is not undefined

答案 6 :(得分:2)

如果必须使用 map 返回自定义输出,您仍然可以将其与 filter 结合使用。

const arr = ['a','b',1]

const result = arr.map(element => {

  if(typeof element === 'string')
    return element + ' something'

}).filter(Boolean) // this will filter out null and undefined

console.log(result) // output: ['a something', 'b something']

答案 7 :(得分:1)

您可以像下面的逻辑一样实现。 假设您想要一个值数组。

let test = [ {name:'test',lastname:'kumar',age:30},
             {name:'test',lastname:'kumar',age:30},
             {name:'test3',lastname:'kumar',age:47},
             {name:'test',lastname:'kumar',age:28},
             {name:'test4',lastname:'kumar',age:30},
             {name:'test',lastname:'kumar',age:29}]

let result1 = test.map(element => 
              { 
                 if (element.age === 30) 
                 {
                    return element.lastname;
                 }
              }).filter(notUndefined => notUndefined !== undefined);

output : ['kumar','kumar','kumar']

答案 8 :(得分:0)

如果你这样使用它,你的问题就迎刃而解了。 此外,您将拥有一个干净而简短的代码

var _ = require('lodash'); //but first, npm i lodash --save
var arr = ['a','b',1];
var results = _.compact(
    _.map(arr, function(item){
        if(_.isString(item)){return item;}
    }
); //false, null, undefined ... etc will not be included

使用 ES6...

const _ = require('lodash'); //but first, npm i lodash --save
const arr = ['a','b',1];
const results = _.compact(
    _.map(arr, item => {
        if(_.isString(item)){return item;}
    }
);
相关问题