用相同的关键名称声明对象
有没有办法在JS中声明具有相同密钥名称的对象?
let Obj = {
'1' : '1',
'1' : '1',
'1' : '1',
}
Is there a way to declare a Object with the same key names in js ?
let Obj = {
'1' : '1',
'1' : '1',
'1' : '1',
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否。对象中属性的名称必须是唯一的,尤其是您可以识别要获得/设置的属性。尽管您编写的代码将解析和运行,但结果对象将仅具有一个属性(最后一个使用该名称):
如果关键名称必须相同,则可以使用一系列对象:
...或者,实际上,只是一个数组(
let array = [1,1,1];
)作为密钥名称如果总是相同的话,没有做任何有用的事情。No. The names of the properties in an object must be unique, not least so you can identify the property you want to get/set. Although the code you wrote will parse and run, the resulting object will have only one property (the last one using that name):
You could use an array of objects if the key name must be the same:
...or, really, just an array (
let array = [1, 1, 1];
) as the key name isn't doing anything useful if it's always the same.