我一直在试图做一个函数,将不会返回任何值,如果两个值是两个数组之间相同。我在这里和谷歌上搜索了几个问题,我发现的一切都表明函数必须返回一个值,并且没有办法避免这样做。所以我直接问是否有办法,如果没有,我该如何解决我想要做的事情?JavaScript有一个函数返回没有值
var bannedValues = [2, 4, 6, 8]; //Even numbers 1-8
var inputValues = [1, 2, 3, 4, 5, 6, 7, 8]; //All numbers 1-8
var filterValues = function(value)
{
for (var number of bannedValues)
{
if (value == number)
{
return; //I want this to return absolutely nothing, not even an empty value
}
}
return value;
}
var filteredValues = inputValues.map(filterValues);
console.log(filteredValues);
/* The array just gains "undefined" values in the places I want there
to be no value, so the array is a length of 8 instead of the 4 I want.
Can I somehow take those out or can I just not have them there in the first place? */
你有处理它返回undefined。 – epascarello