迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中将数组中每个单词首字母大写

作者:迹忆客 最近更新:2023/01/30 浏览次数:

JavaScript 将数组中每个单词的首字母大写:

  1. 使用 Array.map() 方法迭代数组。
  2. 使用 toUpperCase() 方法将每个元素的首字母大写。
  3. 使用 slice() 方法将字符串的其余部分附加到结果中。
function capitalizeWords(arr) {
  return arr.map(word => {
    const firstLetter = word.charAt(0).toUpperCase();
    const rest = word.slice(1).toLowerCase();

    return firstLetter + rest;
  });
}

// 👇️ [ 'Fql', 'Jiyik' ]
console.log(capitalizeWords(['fql', 'jiyik']));

// 👇️ [ 'Fql', 'Jiyik', 'Com' ]
console.log(capitalizeWords(['fql', 'jiyik', 'com']));

JavaScript 中将数组中每个单词首字母大写

capitalizeWords 函数接受一个数组并将每个数组元素的第一个字母大写。

我们传递给 Array.map 方法的函数被数组中的每个元素(单词)调用。

map() 方法返回一个新数组,其中包含从回调函数返回的值。

在每次迭代中,我们访问索引 0 处的字符并使用 String.toUpperCase 方法将其转换为大写。

console.log('jiyik'.charAt(0)); // 👉️ j
console.log('jiyik'.charAt(0).toUpperCase()); // 👉️ J

String.charAt 方法返回指定索引处的字符。

如果字符串中不存在索引,则该方法返回一个空字符串。

最后一步是获取字符串的其余部分并将其添加到大写的第一个字符。

// 👇️ Jiyik
console.log(str.charAt(0).toUpperCase() + str.slice(1).toLowerCase());

我们使用 String.toLowerCase 方法将字符串的其余部分转换为小写。

如果我们不想将字符串的其余部分转为小写,则无需调用该方法。

String.slice() 方法提取字符串的一部分并将其返回,而不修改原始字符串。

String.slice() 方法采用以下参数:

  • start 索引 要包含在返回的子字符串中的第一个字符的索引
  • end 索引 要从返回的子字符串中排除的第一个字符的索引

当只有一个参数被传递给 String.slice() 方法时,切片会到达字符串的末尾。

我们使用参数 1 作为起始索引,以在字符串的第二个字符处开始切片。

JavaScript 索引在 JavaScript 中是从零开始的。 字符串中第一个字符的索引为 0,最后一个字符的索引为 str.length - 1

我们的函数不会改变原始数组的内容。 它返回一个新数组。

或者,我们可以使用 Array.forEach() 方法。


使用 forEach() 将数组中每个单词的第一个字母大写

将数组中每个单词的首字母大写:

  1. 使用 Array.forEach() 方法迭代数组。
  2. 使用 toUpperCase() 方法将每个单词的首字母大写。

更新当前索引处的数组元素。

const arr = ['fql', 'jiyik', 'com'];

arr.forEach((word, index) => {
  const firstLetter = word.charAt(0).toUpperCase();
  const rest = word.slice(1).toLowerCase();

  arr[index] = firstLetter + rest;
});

// 👇️ [ 'Fql', 'Jiyik', 'Com' ]
console.log(arr);

我们传递给 Array.forEach 方法的函数会针对数组中的每个元素进行调用。

forEach() 方法返回 undefined,所以我们必须执行某种变异来保持状态。

在每次迭代中,我们使用 toUpperCase() 方法将当前单词的首字母转换为大写,并使用 slice() 方法获取单词的其余部分。

最后一步是使用索引更新当前数组元素。

我们也可以使用基本的 for 循环来获得相同的结果。

使用 for 将数组中每个单词的第一个字母大写

将数组中每个单词的首字母大写:

  1. 使用 for 循环遍历数组。
  2. 使用 toUpperCase() 方法将每个单词的首字母大写。
  3. 更新当前索引处的数组元素。
const arr = ['fql', 'jiyik', 'com'];

for (let index = 0; index < arr.length; index++) {
  const firstLetter = arr[index].charAt(0).toUpperCase();
  const rest = arr[index].slice(1).toLowerCase();

  arr[index] = firstLetter + rest;
}

// 👇️ [ 'Fql', 'Jiyik', 'Com' ]
console.log(arr);

我们使用了一个基本的 for 循环来遍历单词数组。

在每次迭代中,我们将索引 0 处的字母大写,将单词的其余部分转换为小写并附加两个字符串。

选择哪种方法是个人喜好的问题。 我会使用 Array.map() 方法,因为我发现它非常直接和直观。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便