具有相同键的多个对象 [英] multiple objects with the same key

查看:99
本文介绍了具有相同键的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组:

[object, object, object, object, object]

我想使用对象标题为每个对象设置一个键:

that i want to set a key to each using the object title:

{test:object, tester:object, foo:object, bar:object, test:object}

所以我可以说array.test而不是必须做array[0].title. 但是,密钥可以是通用的,并且当有多个具有相同密钥的对象时,它将替换原始密钥.

so i could say array.test instead of having to do array[0].title. However the key can be generic, and when there are multiple objects with the same key it replaces the original.

在不向索引添加索引值的情况下,我想做的事是不可能的吗?在这种情况下,最原始的解决方案是我最初的array[0].title.

Is what i want to do impossible without adding an index value to the key? In which case is the tidiest solution what i originally had array[0].title.

推荐答案

有没有办法为多个对象设置相同的键,而无需 除了将索引值附加到键之外,还可以替换它?

is there any way to set the same key to multiple objects without replacing it other than appending an index value to to the key?

.对象键是唯一的,这意味着一个键只能包含一个值.如果您想保留多个值,则可以为其分配数组值.像{ key : [value1, value2 ...]}一样,但这对您的问题毫无用处.另外,您不确定什么是关键值,那是对象/地图的整个目标.

No. The object keys are unique, meaning a key can hold only one value. If you like to hold multiple value, then you assign array value to it. like { key : [value1, value2 ...]} but this is of no use for your problem. Also you are not sure what is the key value, and that is the whole objective of the object/map.

即时通讯是最好的方式吗?

is the way im doing it the best way?

.对于您提到的使用数组数据结构/构造的问题,这是很好的.

Yes. For the problem you mentioned using array data-structure/construct is good.

这篇关于具有相同键的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆