Javascript forEach 中的三元运算符 returns 未定义
Javascript ternary operator inside forEach returns undefined
我正在尝试编写一个函数,它接受一个字符串并将每个未包含在“minorWords”字符串中的单词的首字母大写。我的代码中缺少什么导致 return 值为“未定义”?在以几种不同的方式编写此函数之后,我现在认为我只是错误地使用了 .forEach。我相当确定我正在适当地使用三元运算符,但我尝试替换 if 语句并得到相同的结果( undefined )。我也不确定为什么 undefined 被 returned 两次。 . .
function titleCase1(title, minorWords) {
var titleArray = title.split(" ");
var minorArray = minorWords.split(" ");
var newArray = titleArray.forEach(function(word){
(word in minorArray)? word :
word[0].toUpperCase() + word.slice(1);
})
console.log(newArray);
}
titleCase1("the wind in the willows", "the in");
// -> undefined undefined
我知道如果这行得通,第一个 "the" 将不会被大写,但一旦我不再滥用这里的工具,我就会解决这个问题。 . .
您的代码有两个问题:
forEach
唯一做的就是对数组中的每个元素执行回调,而不是 return 任何东西,因此 newArray
将始终是 undefined
。作为参考,检查 forEach
是如何工作的 here。
如果您想创建一个新数组,其值与您尝试使用 newArray
的方式相同。您需要使用 map
,但实际上您需要 return 来自回调的值。作为参考,检查 map
是如何工作的 here.
您不能使用 in
运算符来查看数组中是否存在单词。 in
运算符仅检查指定的 属性 是否存在于指定的对象中。因此,当用于检查数组内部的元素时,它总是 return false
。 因为javascript中的数组实际上是一个对象!
var = [
'a',
'b',
'c'
];
实际上是
var a = {
0: 'a',
1: 'b',
2: 'c'
};
因此 'a' in [ 'a', 'b', 'c' ]
将始终 return false
例如 0 in [ 'a', 'b', 'c' ]
将 return true
.
由于这个警告,您应该改变您的方法,例如使用 indexOf
。作为参考,检查 indexOf
是如何工作的 here.
考虑到这一点,您可以将代码修改为以下内容以获得所需的行为:
function titleCase1(title, minorWords) {
var titleArray = title.split(' ');
var minorArray = minorWords.split(' ');
var newArray = titleArray.map(function (word) {
// indexOf returns the elements index on match or -1.
var match = minorArray.indexOf(word) != -1;
// If there's a match, return the (lowercased) word, otherwise uppercase it.
return match ? word : (word[0].toUpperCase() + word.slice(1));
});
console.log(newArray);
}
titleCase1("the wind in the willows", "the in"); // [ 'the', 'Wind', 'in', 'the', 'Willows' ]
我正在尝试编写一个函数,它接受一个字符串并将每个未包含在“minorWords”字符串中的单词的首字母大写。我的代码中缺少什么导致 return 值为“未定义”?在以几种不同的方式编写此函数之后,我现在认为我只是错误地使用了 .forEach。我相当确定我正在适当地使用三元运算符,但我尝试替换 if 语句并得到相同的结果( undefined )。我也不确定为什么 undefined 被 returned 两次。 . .
function titleCase1(title, minorWords) {
var titleArray = title.split(" ");
var minorArray = minorWords.split(" ");
var newArray = titleArray.forEach(function(word){
(word in minorArray)? word :
word[0].toUpperCase() + word.slice(1);
})
console.log(newArray);
}
titleCase1("the wind in the willows", "the in");
// -> undefined undefined
我知道如果这行得通,第一个 "the" 将不会被大写,但一旦我不再滥用这里的工具,我就会解决这个问题。 . .
您的代码有两个问题:
forEach
唯一做的就是对数组中的每个元素执行回调,而不是 return 任何东西,因此newArray
将始终是undefined
。作为参考,检查forEach
是如何工作的 here。如果您想创建一个新数组,其值与您尝试使用
newArray
的方式相同。您需要使用map
,但实际上您需要 return 来自回调的值。作为参考,检查map
是如何工作的 here.您不能使用
in
运算符来查看数组中是否存在单词。in
运算符仅检查指定的 属性 是否存在于指定的对象中。因此,当用于检查数组内部的元素时,它总是 returnfalse
。 因为javascript中的数组实际上是一个对象!var = [ 'a', 'b', 'c' ];
实际上是
var a = { 0: 'a', 1: 'b', 2: 'c' };
因此
'a' in [ 'a', 'b', 'c' ]
将始终 returnfalse
例如0 in [ 'a', 'b', 'c' ]
将 returntrue
.由于这个警告,您应该改变您的方法,例如使用
indexOf
。作为参考,检查indexOf
是如何工作的 here.
考虑到这一点,您可以将代码修改为以下内容以获得所需的行为:
function titleCase1(title, minorWords) {
var titleArray = title.split(' ');
var minorArray = minorWords.split(' ');
var newArray = titleArray.map(function (word) {
// indexOf returns the elements index on match or -1.
var match = minorArray.indexOf(word) != -1;
// If there's a match, return the (lowercased) word, otherwise uppercase it.
return match ? word : (word[0].toUpperCase() + word.slice(1));
});
console.log(newArray);
}
titleCase1("the wind in the willows", "the in"); // [ 'the', 'Wind', 'in', 'the', 'Willows' ]