将对象的所有键转换为小写的最有效方法是什么 - JavaScript?
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/15 1:16:17
假设以下是我们的对象 −
var details =
{
"STUDENTNAME": "John",
"STUDENTAGE": 21,
"STUDENTCOUNTRYNAME": "US"
}
如上所示,键为大写。我们需要将所有这些键转换为小写。为此使用 toLowerCase()。
示例
以下是代码 −
var details =
{
"STUDENTNAME": "John",
"STUDENTAGE": 21,
"STUDENTCOUNTRYNAME": "US"
}
var tempKey, allKeysOfDetails = Object.keys(details);
var numberOfKey = allKeysOfDetails.length;
var allKeysToLowerCase = {}
while (numberOfKey--) {
tempKey = allKeysOfDetails[numberOfKey];
allKeysToLowerCase[tempKey.toLowerCase()] = details[tempKey];
}
console.log(allKeysToLowerCase);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,文件名是 demo297.js。
输出
这将在控制台上产生以下输出 −
PS C:\Users\Amit\javascript-code> node demo297.js
{ studentcountryname: 'US', studentage: 21, studentname: 'John' }
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

