javascript 为什么javascript map 函数返回undefined?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16037049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:00:18  来源:igfitidea点击:

Why does javascript map function return undefined?

javascript

提问by Akshat Jiwan Sharma

My code

我的代码

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

This gives the following results

这给出了以下结果

["a","b",undefined]

I don't want undefined in the results array.How can I do it?

我不想在结果数组中使用 undefined。我该怎么做?

回答by Ikke

You aren't returning anything in the case that the item is not a string. In that case, the function returns undefined, what you are seeing in the result.

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

The map function is used to map one value to another, but it looks you actually want to filter the array, which a map function is not suitable for.

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

What you actually want is a filterfunction. It takes a function that returns true or false based on whether you want the item in the resulting array or not.

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

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

回答by Nicola Pedretti

Filter works for this specific case where the items are not modified. But in many cases when you use map you want to make some modification to the items passed.

过滤器适用于不修改项目的这种特定情况。但是在很多情况下,当您使用 map 时,您希望对传递的项目进行一些修改。

if that is your intent, you can use reduce:

如果这是您的意图,您可以使用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
}, [])

回答by Matas Vaitkevicius

Since ES6 filtersupports pointy arrow notation (like LINQ):

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

So it can be boiled down to following one-liner.

所以它可以归结为以下单行。

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

回答by Jojo Narte

My solution would be to use filter after the map.

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

This should support every JS data type.

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

example:

例子:

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

回答by BenM

You only return a value if the current element is a string. Perhaps assigning an empty string otherwise will suffice:

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

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

Of course, if you want to filter any non-string elements, you shouldn't use map(). Rather, you should look into using the filter()function.

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

回答by Prasath K

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

回答by mkumar

You can implement like a below logic. Suppose you want an array of values.

您可以实现如下逻辑。假设您想要一个值数组。

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']