是否可以在 JavaScript 中显示对象条目中的子字符串?
javascriptweb developmentobject oriented programming更新于 2025/10/30 22:36:17
可以,您可以将 Object.fromEntries() 与 substr() 一起使用。在 substr() 下,请提及子字符串的起始索引和长度。
示例
const originalString = {
"John 21 2010" :1010,
"John 24 2012" :1011,
"John 22 2014" :1012,
"John 22 2016" :1013,
}
const result = Object.fromEntries(Object.entries(originalString).
map(([k, objectValue])=>
[k.substr(0, k.length-5), objectValue]));
console.log(result)
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,文件名是 demo41.js。
输出
这将产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo41.js
{ 'John 21': 1010, 'John 24': 1011, 'John 22': 1013 }
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

