尝试将字符串数组中的第一个字符大写,为什么这不起作用?
我正在尝试编写一个函数,将 list-style-image
转换为 listStyleImage
。
我想出了一个功能,但它似乎不起作用。有人能指出我这里的问题吗?
var myStr = "list-style-image";
function camelize(str){
var newStr = "";
var newArr = [];
if(str.indexOf("-") != -1){
newArr = str.split("-");
for(var i = 1 ; i < newArr.length ; i++){
newArr[i].charAt(0).toUpperCase();
}
newStr = newArr.join("");
}
return newStr;
}
console.log(camelize(myStr));
I'm trying to write a function that converts for example list-style-image
to listStyleImage
.
I came up with a function but it seems not working. Can anybody point me to the problem here ?
var myStr = "list-style-image";
function camelize(str){
var newStr = "";
var newArr = [];
if(str.indexOf("-") != -1){
newArr = str.split("-");
for(var i = 1 ; i < newArr.length ; i++){
newArr[i].charAt(0).toUpperCase();
}
newStr = newArr.join("");
}
return newStr;
}
console.log(camelize(myStr));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您实际上必须重新分配数组元素:
“toUpperCase()”函数返回新字符串,但不会修改原始字符串。
您可能需要首先检查以确保
newArr[i]
是空字符串,以防您得到带有两个连续破折号的输入字符串。编辑 - 指出SO贡献者@lonesomeday正确地指出你还需要将每个字符串的其余部分粘回去:
You have to actually re-assign the array element:
The "toUpperCase()" function returns the new string but does not modify the original.
You might want to check to make sure that
newArr[i]
is the empty string first, in case you get an input string with two consecutive dashes.edit — noted SO contributor @lonesomeday correctly points out that you also need to glue the rest of each string back on:
这是我使用 ES6 的解决方案。这是一个示例,我将一周中的几天存储在数组中,并使用
for...of
循环将它们大写。以下是文档的链接:for ... of 循环文档
Here is my solution with ES6. This is an example where I store the days of the week in my array and I uppercase them with
for... of
loop.Here is a link to the documentation: for... of loop documentation
在
for
循环中,您需要替换newArr[i]
的值,而不是简单地计算它:In your
for
loop, you need to replace the value ofnewArr[i]
instead of simply evaluating it:从 JavaScript ES6 开始,您可以用一行实现字符串数组的“驼峰化”:
Since JavaScript ES6 you can achieve the "camelization" of an array of strings with a single line:
这是我使用 slice() 方法的解决方案。
})
console.log(capitalized)
// 预期输出:
// [“爱丽丝”、“鲍勃”、“查理”、“丹妮尔”]
Here is my solution using the slice() method.
})
console.log(capitalized)
// Expected output:
// ["Alice", "Bob", "Charlie", "Danielle"]
您不需要数组来将连字符和小写字母替换为大写字母 -
You don't need the array to replace a hyphen and a lowercase letter with the uppercase-
您需要将大写字母存储回数组中。请参考下面修改后的循环,
you need to store the capitalized letter back in the array. Please refer the modified loop below,
有点长,但它基本上可以通过数组完成工作:
A little bit longer but it gets the job done basically playing with arrays:
substr()
方法返回字符串中起始索引与其后的多个字符之间的部分。来源:https://developer.mozilla。 org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substrThe
substr()
method returns the part of a string between the start index and a number of characters after it. Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr这是另一个解决方案,尽管我玩得太晚了。
Here's another solution though I am so late in the game.
我们来看看下面的代码。
输出: [“周日”,“周一”]
Let's checkout the code below.
Output: ["Sun", "Mon"]
我最好的方法是:
my best way is:
以下是我的做法。
Here is how I would go about it.
请检查下面的代码
Please check this code below
const 名称 = ['鲍勃', '结婚', '爱丽丝', '詹妮弗'];
const 大写 = 名称.map((名称) => {
返回(
name[0].charAt(0).toUpperCase() + name.slice(1) + ' '
)
});
console.log(大写)
const names = ['bob', 'marry', 'alice', 'jenifer'];
const capitalized = names.map((name) => {
return(
name[0].charAt(0).toUpperCase() + name.slice(1) + ' '
)
});
console.log(capitalized)