简体   繁体   English

Snake to Camel Case:[已解决] 我被困在第二个字符串中,当我连接时不能跳过第一个字符,不要重复 de char[0]

[英]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]

The function only can solved with.map, i cant find the way to skip the first index of each string to solve the excercise. 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'

` `

This is my best trie, i google for answers but i dont find nothing with.map method, if anyone know where are my error i been so grateful.这是我最好的 trie,我用谷歌搜索答案,但我没有找到任何东西。map 方法,如果有人知道我的错误在哪里,我将非常感激。 Thanks!谢谢!

PD: I can't use.replace! PD:我不能使用。更换!

I SOLVED, REPLACING THE "... + char.toLowerCase()" to "char.substring(1).toLowerCase()"我解决了,将“... + char.toLowerCase()”替换为“char.substring(1).toLowerCase()”

Rather than index specifiers [] , I would use slice for this..而不是索引说明符[] ,我会为此使用 slice ..

char.slice(0,1) get first char, char.slice(1) , get chars from index 1 to end.. char.slice(0,1)获取第一个字符, char.slice(1)获取从索引 1 到结束的字符。

eg.例如。

 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.

相关问题 我正在尝试每 2 秒将一个字符添加到一个字符串中,但它不起作用,控制台中没有错误。 视图 - I'm trying adding a to a string a char every 2 seconds, but it diesn't work, no errors in a console. Vue 在React JS中,我正在尝试创建一个倒数计时器,但无法将第一秒变成一整秒 - In React JS I'm trying to create a countdown timer but can't get the first second to be a full second 我无法解决网络问题的代码中的angularjs错误无法正常工作 - angularjs error in the code that I can't solved the web don't works 为什么我不能用Typescript(Javascript)替换换行符Char - Why can't I replace a newline Char with Typescript (Javascript) 我如何不重复部分代码 - How can I don't repeat a part of code Javascript-我无法在字符串的开头连接 - Javascript - I can't concatenate at the beginning of my string 字符串中的字符串不能用换行符分割 - String within a string can't be split by newline char 带有 ESCPOS 库的土耳其字符不起作用 - Turkish char with ESCPOS library don't work 按字符串拆分字符串,但跳过某些字符组合 - Split string by char, but skip certain char combinations 我正在尝试使用async / await来获取服务,但是第二个服务返回并没有填充我的变量 - I'm trying to use async/await to get a service, but the second service returns don't fill my variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM