Javascript Lodash 标题大小写(每个单词的大写首字母)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38084396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:04:01  来源:igfitidea点击:

Lodash title case (uppercase first letter of every word)

javascriptlodash

提问by brandonscript

I'm looking through the lodash docs and other Stack Overflow questions - while there are several native JavaScript ways of accomplishing this task, is there a way I can convert a string to title case using purelylodash functions (or at least existing prototypal functions) so that I don't have to use a regular expression or define a new function?

我正在查看 lodash 文档和其他 Stack Overflow 问题 - 虽然有几种原生 JavaScript 方法可以完成此任务,但有没有一种方法可以使用纯粹的lodash 函数(或至少现有的原型函数)将字符串转换为标题大小写这样我就不必使用正则表达式或定义新函数?

e.g.

例如

This string ShouLD be ALL in title CASe

should become

应该成为

This String Should Be All In Title Case

回答by 4castle

This can be done with a small modification of startCase:

这可以通过一个小的修改来完成startCase

_.startCase(_.toLower(str));

console.log(_.startCase(_.toLower("This string ShouLD be ALL in title CASe")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

回答by aristidesfl

_.startCase(_.camelCase(str))

For non-user-generated text, this handles more cases than the accepted answer

对于非用户生成的文本,这比接受的答案处理更多的情况

> startCase(camelCase('myString'))
'My String'
> startCase(camelCase('my_string'))
'My String'
> startCase(camelCase('MY_STRING'))
'My String'
> startCase(camelCase('my string'))
'My String'
> startCase(camelCase('My string'))
'My String'

回答by James Nguyen

with lodash version 4.

使用 lodash 版本 4。

_.upperFirst(_.toLower(str))

_.upperFirst(_.toLower(str))

回答by CD..

'This string ShouLD be ALL in title CASe'
  .split(' ')
  .map(_.capitalize)
  .join(' ');

回答by Vishal Shetty

There are mixed answers to this question. Some are recommending using _.upperFirstwhile some recommending _.startCase.

这个问题有多种答案。有的推荐使用_.upperFirst,有的推荐使用_.startCase

Know the difference between them.

知道它们之间的区别。

i) _.upperFirstwill transform the first letter of your string, then string might be of a single word or multiple words but the only first letter of your string is transformed to uppercase.

i)_.upperFirst将转换您的字符串的第一个字母,然后字符串可能是单个单词或多个单词,但您的字符串的唯一第一个字母被转换为大写。

_.upperFirst('jon doe')

_.upperFirst('jon doe')

output:

输出:

Jon doe

Jon doe

check the documentation https://lodash.com/docs/4.17.10#upperFirst

检查文档https://lodash.com/docs/4.17.10#upperFirst

ii) _.startCasewill transform the first letter of every word inside your string.

ii)_.startCase将转换字符串中每个单词的第一个字母。

_.startCase('jon doe')

_.startCase('jon doe')

output:

输出:

Jon Doe

Jon Doe

https://lodash.com/docs/4.17.10#startCase

https://lodash.com/docs/4.17.10#startCase

回答by Brandon

Here's a way using ONLY lodash methods and no builtin methods:

这是一种只使用 lodash 方法而不使用内置方法的方法:

_.reduce(_.map(_.split("Hello everyOne IN the WOrld", " "), _.capitalize), (a, b) => a + " " + b)

回答by Luke Robertson

const titleCase = str =>
  str
    .split(' ')
    .map(str => {
      const word = str.toLowerCase()
      return word.charAt(0).toUpperCase() + word.slice(1)
    })
    .join(' ')

You can also split out the map function to do separate words

也可以拆分map函数做单独的词

回答by Justin Brown

This can be done with only lodash

这可以只用 lodash 来完成

properCase = string =>
        words(string)
            .map(capitalize)
            .join(' ');

const proper = properCase('make this sentence propercase');

console.log(proper);
//would return 'Make This Sentence Propercase'

回答by Tarun Majumder

Below code will work perfectly:

下面的代码将完美运行:

var str = "TITLECASE";
_.startCase(str.toLowerCase());

回答by m-a-r-c-e-l-i-n-o

Not as concise as @4castle's answer, but descriptive and lodash-full, nonetheless...

不像@4castle 的回答那么简洁,但描述性和 lodash-full,尽管如此......

var basicTitleCase = _
    .chain('This string ShouLD be ALL in title CASe')
    .toLower()
    .words()
    .map(_.capitalize)
    .join(' ')
    .value()

console.log('Result:', basicTitleCase)
console.log('Exact Match:' , basicTitleCase === 'This String Should Be All In Title Case')
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>