为什么javascript map函数返回undefined?

Aks*_*rma 91 javascript

我的代码

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

这给出了以下结果

["a","b",undefined]
Run Code Online (Sandbox Code Playgroud)

我不想在结果数组中使用undefined.我怎么能这样做?

Ikk*_*kke 154

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

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

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

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

  • 啊...我不知道有过滤功能。谢谢。 (2认同)

Mat*_*ius 14

由于ES6 filter支持尖箭头表示法(如LINQ):

所以它可以归结为跟随一线.

['a','b',1].filter(item => typeof item ==='string');
Run Code Online (Sandbox Code Playgroud)


Nic*_*tti 14

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

如果这是你的意图,你可以使用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
}, [])
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 - “map” 会产生带有“undefined”的数组。`filter` 只返回项目或不返回项目。太棒了 (3认同)

Fat*_*lut 11

如果您必须使用映射返回自定义输出,您仍然可以将其与过滤器结合使用。

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']
Run Code Online (Sandbox Code Playgroud)


小智 9

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

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']
Run Code Online (Sandbox Code Playgroud)


Ben*_*enM 8

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

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

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

  • 如果存在数字,则返回空字符串 (2认同)

Joj*_*rte 7

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

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

例:

const notUndefined = anyValue => typeof anyValue !== 'undefined'    
const noUndefinedList = someList
          .map(// mapping condition)
          .filter(notUndefined); // by doing this, 
                      //you can ensure what's returned is not undefined
Run Code Online (Sandbox Code Playgroud)


Pra*_*h K 5

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