简体   繁体   English

每个单词的首字母大写使用

[英]Capitalize the first letter of each word using

I'm doing a class on javascript this semester, and one of this week's exercises was to take a string of words, and capitalize the first letter of each word. 我本学期正在使用javascript上课,本周的练习之一是采用一串单词,并大写每个单词的第一个字母。 I did that using .map() , the code is here : 我使用.map()做到了,代码在这里:

 let t1 = "hello how are you doing".split(" "); let t2 = t1.map(function(word) { return word[0].toUpperCase() + word.slice(1); }); console.log(t2.join(" ")); 

And it works perfectly fine. 而且效果很好。 However, I was wondering why, when I try with forEach() , I can't make it work. 但是,我想知道为什么当我尝试使用forEach() ,无法使其正常工作。 Here is my code : 这是我的代码:

 let t1 = "hello how are you doing".split(" "); t1.forEach(function(word) { word[0] = word[0].toUpperCase(); }) console.log(t1.join(" ")); 

My understanding of forEach() is that it cycles through every element of the table, much like a simple for loop. 我对forEach()理解是,它遍历表的每个元素,就像一个简单的for循环。 So then shouldn't my code take the first letter of each word, and replace it with the same letter, to which toUpperCase() is applied? 因此,我的代码难道不应该将每个单词的第一个字母替换为toUpperCase()应用到的相同字母吗?

edit : I already know how to capitalize the first letter of each word, I was just asking about the different methods 编辑:我已经知道如何大写每个单词的第一个字母,我只是在问不同的方法

 let word = 'foo'; word[0] = 'F'; console.log(word); 

The modification of word doesn't take because strings are immutable. 不需要修改word ,因为字符串是不可变的。 You can't change a single character inside a string. 您不能在字符串中更改单个字符。

For character access using bracket notation, attempting to delete or assign a value to these properties will not succeed. 对于使用括号符号的字符访问,尝试删除或为这些属性分配值将不会成功。 The properties involved are neither writable nor configurable. 所涉及的属性既不可写也不可配置。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String

First, string in JS is immutable. 首先,JS中的字符串是不可变的。

 var str = 'hello World'; str[0] = 'H'; console.log(str) 

So word[0] = will not have any effect. 因此word[0] =不会有任何效果。

Second, even if it was, you are updating a value of an argument variable and not value in array. 其次,即使是更新,也要更新参数变量的值,而不是数组中的值。

 var a = [1, 2, 3]; a.forEach(function(n) { n = n * 2; }); console.log(a) 

As per discussion with @ deceze , this point is not accurate. 根据与@ deceze的讨论,这一点是不正确的。

If a string was mutable, that would have worked just fine. 如果字符串是可变的,那会很好用。 Your "Second" doesn't really apply. 您的“第二个”实际上并不适用。 deceze 降低


@deceze Objects are assigned using reference, so it will have the effect. @deceze对象是使用引用分配的,因此它将起作用。 Primitive values are assigned using value. 使用值分配原始值。 So n in my understanding will always be a copy of item in array. 因此,根据我的理解,n始终是数组中项目的副本。 Any manipulation relating to it should not affect the item in array. 与之相关的任何操作都不应影响数组中的项目。 And string being a primitive value, I mentioned it. 我提到过字符串是原始值。 Also, I can't think of any variable that has property and is of primitive type. 另外,我想不出具有属性并且是原始类型的任何变量。 If you have any idea please let me know. 如果您有任何想法,请告诉我。 Would be glad to learn. 很高兴学习。 :-) – Rajesh :-) – 拉杰什


Sure, you're right about that. 当然,您是对的。 That's why I'm saying if it was mutable in the first place… Since it's not, the point is moot either way. 这就是为什么我首先要说的是可变的……因为不是,所以无论如何都没有意义。 deceze 降低


To get the desired effect, you will have to override value in array. 为了获得理想的效果,您将不得不覆盖数组中的值。

 let t1 = "hello how are you doing".split(" "); t1.forEach(function(word, index) { t1[index] = word[0].toUpperCase() + word.substring(1); }) console.log(t1.join(" ")); 

Reference: 参考:

So then shouldn't my code take the first letter of each word, and replace it with the same letter, to which toUpperCase() is applied? 因此,我的代码难道不应该将每个单词的第一个字母替换为toUpperCase()应用到的相同字母吗?

Because word is a primitive value hence when you set new value to word , it doesn't carry the reference back to the array. 因为word是原始值,所以当您将新值设置为word ,它不会将引用带回到数组中。

However, if parameter of forEach is not a primitive value, then the original one gets mutated, see the demo below 但是,如果forEach参数不是原始值,则原始值会发生突变,请参见下面的演示

 var arr = [[1,2,3],[4,5,6],[7,8,9]]; arr.forEach(function(item){ item.push(1); }) console.log(arr); 

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

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