简体   繁体   English

在Array.map上返回undefined

[英]Return undefined on Array.map

I don't want this map to return undefined how can i do that ? 我不希望这张地图返回未定义我该怎么做?

var onCompareSelectedClick = function () {
            var talentProfileInfoForAppliedResources = appliedResourcesEntries.map(function(res) {
                console.log(res);
                if(res.compareSelected == true) {
                    return data.getTalentProfileInfo(res.RES.RES_ID);
                }
            });
            console.log(talentProfileInfoForAppliedResources);
            this.openCompareTPDlg(talentProfileInfoForAppliedResources);
        }.bind(this);

Just add else statement inside map method returning needed value, like: 只需在map方法中添加else语句,返回所需的值,如:

if(res.compareSelected == true) {
   return data.getTalentProfileInfo(res.RES.RES_ID);
} else {
   return 'default_value';
}

TL;DR TL; DR

Use the Array.filter method after Array.map to remove undefined elements in the new array. 在Array.map之后使用Array.filter方法删除新数组中的未定义元素。


Expanding on @Bloomca's answer: 扩展@ Bloomca的答案:

As stated in the documentation provided here . 如此处提供的文档中所述

The map() method creates a new array with the results of calling a provided function on every element in this array. map()方法创建一个新数组,其结果是在此数组中的每个元素上调用提供的函数。

Hence the reason why your new array contains undefined elements is because you are not explicitly calling return within the function on some elements that are called using the provided function. 因此,您的新数组包含未定义元素的原因是因为您没有在使用提供的函数调用的某些元素的函数内显式调用return。 In Javascript, not explicitly calling return will nevertheless return undefined . 在Javascript中,不显式调用return将返回undefined

For example, in the following method newArray will be set to the logged result: 例如,在以下方法中,newArray将设置为记录的结果:

[ undefined, 2, 3 ]

newArray = [1,2,3].map(function(elem) { if (elem > 1) return elem })
console.log(newArray)

This is why the answer provided above will no longer result in undefined elements within the new array. 这就是为什么上面提供的答案将不再导致新数组中undefined元素。 The conditional will resolve if the condition res.compareSelected == true is not true to the return statement within the else block (note that you could simply remove the true here and simply put res.compareSelected which would be better practice). 如果条件res.compareSelected == true对于else块中的return语句不是真的,则条件将解析(请注意,您可以简单地删除此处的true并简单地放入res.compareSelected ,这将是更好的做法)。

Based on your question you may find using the Array.filter method to return an Array without the undefined values. 根据您的问题,您可能会发现使用Array.filter方法返回没有未定义值的Array And with only the values on which you have called the function data.getTalentProfileInfo(res.RES.RES_ID) . 并且只使用您调用函数data.getTalentProfileInfo(res.RES.RES_ID)

You could do this in the following manner: 您可以通过以下方式执行此操作:

var onCompareSelectedClick = function () {
    var arr = appliedResourcesEntries.map(function(res) {
        if(res.compareSelected == true) {
            return data.getTalentProfileInfo(res.RES.RES_ID);
        }
    });
    var talentProfileInfoForAppliedResources = arr.filter(function(elem) {
        return elem;
    });
console.log(talentProfileInfoForAppliedResources);
this.openCompareTPDlg(talentProfileInfoForAppliedResources);
}.bind(this);

You can read about the Array.filter method here. 您可以在此处阅读有关Array.filter方法的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM