![](/img/trans.png)
[英]I'm trying adding a to a string a char every 2 seconds, but it diesn't work, no errors in a console. Vue
[英]Snake to Camel Case: [SOLVED] I'm stuck in the second string, when i concatenate can't skip the first char, to don't repeat de char[0]
function只能用.map來解決,我找不到跳過每個字符串的第一個索引來解決練習的方法。
`
let snakeToCamel = function(string) {
// Your code here
const result = string.split("_").map(char => char[0].toUpperCase() + char.toLowerCase());
return result.join("");
};
console.log(snakeToCamel('snakes_go_hiss')); // 'SnakesGoHiss'
console.log(snakeToCamel('say_hello_world')); // 'SayHelloWorld'
`
這是我最好的 trie,我用谷歌搜索答案,但我沒有找到任何東西。map 方法,如果有人知道我的錯誤在哪里,我將非常感激。 謝謝!
PD:我不能使用。更換!
我解決了,將“... + char.toLowerCase()”替換為“char.substring(1).toLowerCase()”
而不是索引說明符[]
,我會為此使用 slice ..
char.slice(0,1)
獲取第一個字符, char.slice(1)
獲取從索引 1 到結束的字符。
例如。
let snakeToCamel = function(string) { const result = string.split("_").map(char => char.slice(0,1).toUpperCase() + char.slice(1).toLowerCase()); return result.join(""); }; console.log(snakeToCamel('snakes_go_hiss')); // 'SnakesGoHiss' console.log(snakeToCamel('say_hello_world')); // 'SayHelloWorld'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.