简体   繁体   English

用循环和功能javascript将文本更改为驼峰式

[英]Alter text to camel case with loop and function javascript

I'm trying to alter text to camel case with a function. 我正在尝试使用功能将文本更改为驼峰式大小写。 The hyphen in text indicates where the letters need to be capitalized. 文本中的连字符表示字母需要大写。 ie "this-has-been-camel-cased" would become thisHasBeenCamelCased . 也就是说, "this-has-been-camel-cased"将变成thisHasBeenCamelCased I've altered each word and am wanting to return them as a concatenated string using += .However when I try to return the string the += is not recognised or I get an error message stating "fullWord not defined". 我已经更改了每个单词,并希望使用+=将它们作为连接字符串返回。但是,当我尝试返回字符串时,无法识别+=或收到一条错误消息,指出“未定义fullWord”。 This is odd as the var fullWord is recognised when I alert it inside the loop ? 这很奇怪,因为当我在循环内警告它时, var fullWord被识别了吗?

Where is the error coming from here. 错误从何而来。 how can I correct it ? 我该如何纠正?

Thaks Thaks

function camelize(str) {
    var wordArr = str.split('-');
    for (var i = 0; i < wordArr.length; i++) {
        var ltrArr = wordArr[i].split('');
        var firstLtr = ltrArr[0].toUpperCase();
        var endWord = ltrArr.slice(1).join('');
        var fullWord = firstLtr.concat(endWord);
        var newStr += fullword;
    }
    return newStr;
}

alert(camelize('this-has-been-camel-cased')); 

Simpler alternative : 更简单的选择:

'this-has-been-camel-cased'.replace(/-./g, function (m) {
    return m[1].toUpperCase();
});

Here is a function : 这是一个函数:

function camelize(input, splitter) {
    if (!splitter) splitter = '-';
    return input.replace(new RegExp(splitter + '.', 'g'), function (m) {
        return m[1].toUpperCase();
    });
}

And a "no regular expression" version : 和“无正则表达式”版本:

function camelize(input, splitter) {
    var i = 0;
    if (!splitter) splitter = '-';
    input = input.split(splitter);
    while (++i < input.length) {
        input[i] = input[i].charAt(0).toUpperCase() + input[i].slice(1);
    }
    return input.join('');
}

Usage examples : 用法示例:

camelize('az-er-ty') // "azErTy"
camelize('az er ty', ' ') // "azErTy"

Problems I detect on your code at first sight: 我一眼就发现您的代码有以下问题:

  • This code var newStr += fullword; 这段代码var newStr += fullword; is invalid. 是无效的。 You cannot use += when declaring a variable 声明变量时不能使用+=

  • You have to use fullWord and not fullword (notice the casing problem with w 你必须使用fullWord ,而不是fullword (注意与箱体问题w

  • You need to declare (at least) newStr outside of the for loop, otherwise it will be initialized on every iteration. 您需要(至少)在for循环之外声明newStr ,否则它将在每次迭代时初始化。

var newStr += fullword; is redeclared within the loop, declare it outside with an initial value. 在循环内重新声明,在外部用初始值声明。

alternative; 替代;

str = str.replace(/-(.)/g, function($1) { return $1.toUpperCase(); }).replace(/-/g, '');

The problem is indeed with += . 问题的确是+=

It results from the fact that the purpose of += is to add a string or number to another, but in this case you are using it on a variable that does not yet have anything assigned to it: newStr can't have any strings added to it, since it doesn't exist at the time you're invoking it. 其结果是, +=的目的是向另一个字符串或数字添加,但在这种情况下,您将其用于尚未分配任何内容的变量: newStr不能添加任何字符串因为它在您调用它时不存在。

What you need is a direct assignment: 您需要的是直接分配:

var newStr = fullword

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

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