简体   繁体   English

`join()`和多维数组Javascript

[英]`join()` and multidimensional arrays Javascript

I have this code: 我有这个代码:

var arr = str.split(' ');
for(var i=0,  l=arr.length; i<l; i++) {
    arr[i].split('');
    arr[i][0].toUpperCase();
    almost =arr[i].join('');
}

It returns the error " arr[i].join is not a function". 它返回错误“ arr[i].join不是函数”。 I assume I have something wrong with the data types but I have no idea how to fix it. 我假设我的数据类型有问题,但我不知道如何解决它。

When you use arr[i].split() you are not changing original element in array, so when you use arr[i].join('') you get error because arr[i] is still just a string and you can't use join on string. 当你使用arr[i].split()你没有改变数组中的原始元素,所以当你使用arr[i].join('')你会得到错误,因为arr[i]仍然只是一个字符串而你可以join在字符串上使用join Instead you need to assign new value to that element so your code should look like this. 相反,您需要为该元素分配新值,以便您的代码看起来像这样。

 var arr = 'lorem ipsum'.split(' '); for (var i = 0, l = arr.length; i < l; i++) { arr[i] = arr[i].split(''); arr[i][0] = arr[i][0].toUpperCase(); arr[i] = arr[i].join(''); } console.log(arr.join(' ')) 

Or instead you can store that value in variable, change it and then set it to value of current element in loop. 或者您可以将该值存储在变量中,更改它,然后将其设置为循环中当前元素的值。

 var arr = 'lorem ipsum'.split(' '); for (var i = 0, l = arr.length; i < l; i++) { var e = arr[i].split(''); e[0] = e[0].toUpperCase(); arr[i] = e.join('') } console.log(arr.join(' ')) 

Here, I think this is what you're looking for: 在这里,我认为这就是你要找的东西:

var str = 'This is a test string';

var arr = str.split(' ');
for(var i=0,  l=arr.length; i<l; i++) {
    var word = arr[i].split('');
    word[0] = word[0].toUpperCase();
    arr[i] = word.join('');
}
console.log(arr.join(' '));

JSFiddle: https://jsfiddle.net/6f9yoqtg/1/ JSFiddle: https ://jsfiddle.net/6f9yoqtg/1/

It looks like others have explained why join does not work on a string. 看起来其他人已经解释了为什么join不能用于字符串。 However, I noticed that your goal seems to be to uppercase every word in str . 但是,我注意到你的目标似乎是大写str每个单词。 You can use the String#replace method for that, taking a function as the section parameter. 你可以使用String#replace方法,将一个函数作为section参数。

 var string = 'word word words things amazing icons yup' console.log(string) string = string.replace(/(\\S)(\\S*)/g, function (_, first, rest) { return first.toUpperCase() + rest }) console.log(string) 

With ES6, you could use destructuring ans rest syntax. 使用ES6,您可以使用destructuring和rest语法。

 var str = 'This is a test string'; str = str.split(' ').map(([first, ...rest]) => [first.toUpperCase(), ...rest].join('')).join(' '); console.log(str); 

There are a few concepts that you need to consider here. 您需要在此处考虑一些概念。 if I'm not mistaken you are trying to create a function that will capitalize each first letter in a given string. 如果我没有弄错你正试图创建一个函数来大写给定字符串中的每个首字母。

Start by simply creating a function that will take any string as a parameter using the variable str and then later manipulate it. 首先简单地创建一个函数,该函数将使用变量str将任何字符串作为参数,然后再对其进行操作。 Think of the function as a funnel something goes into it and then we have something that will come out the other end as an output. 把这个功能想象成一个漏斗的东西进入它,然后我们有一些东西将作为输出从另一端出来。 make sure you call the function later. 确保稍后调用该功能。

  function capitalize(str) { };
capitalize(every firSt lettEr of the sentence);

Now inside the curly brackets is where the magic happens. 现在大括号里面是魔术发生的地方。 we can use the .toLowerCase(), in case we might have capital letters some where along the string, and then use the .split() to split apart the letters at every empty space if there are many words in the string that we are trying to capitalize. 我们可以使用.toLowerCase(),以防我们可能在字符串中有一些大写字母,然后使用.split()在每个空格中拆分字母,如果字符串中有很多单词我们是试图利用资本。 Remember that after the split() you will no longer have a string but rather a string inside an array. 请记住,在split()之后,您将不再拥有字符串,而是在数组中使用字符串。 Note that if you use 2 empty spaces in the split(' '), you will only capitalize the very first letter of the sentence. 请注意,如果在分割('')中使用2个空格,则只会将句子的第一个字母大写。

function capitalize(str) { str = str.toLowerCase().split(' '); return str;}
//call function 
capitalize(every firSt lettEr of the sentence);
//outputs ["every first letter of the sentence"]

then to iterate through each word in the array using a simple for loop like you have that will look like this: 然后使用一个简单的for循环遍历数组中的每个单词,就像你看到的那样:

for (var i=0; i<str.length; i++){}

in the curly brackets of the loop you will want to add a statement that will take the first letter of each word capitalize it then add the rest of the letters using splice(). 在循环的大括号中,您将要添加一个语句,该语句将每个单词的第一个字母大写,然后使用splice()添加其余字母。 it will go through each word in the array until there are non left, which is why we used the str.length. 它将遍历数组中的每个单词,直到没有左,这就是我们使用str.length的原因。 Lastly you will use the str.join() to put together the all the words in the array, that had a space between them, back into a string. 最后,您将使用str.join()将数组中的所有单词(它们之间有空格)放回一个字符串中。 it will look like this: 它看起来像这样:

function titleCase(str) {
  str = str.toLowerCase().split(' ');
  for (var i = 0; i < str.length; i++) {
    str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); 
}

  return str.join(' ');
}
capitalize("every firSt lettEr of the sentence");

//output "Every First Letter Of The Sentence"

I hope that helped. 我希望有所帮助。 if you need any help with the concepts don't be afraid to ask. 如果你需要任何帮助的概念,不要害怕问。

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

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