javascript 具有重复名称的 JS 关联对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3996135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS associative object with duplicate names
提问by SteeleR
ok, so I have an object like:
好的,所以我有一个对象:
var myobject = {
"field_1": "lorem ipsum",
"field_2": 1,
"field_2": 2,
"field_2": 6
};
as you see there are duplicate names in the object, but with different values. If i go through it like (using jQuery):
如您所见,对象中有重复的名称,但具有不同的值。如果我像(使用 jQuery)那样经历它:
$.each(myobject, function(key, value)
{
console.log(key);
console.log(myobject[key]);
console.log(myobject[value]);
}
key - returns the correct key
myobject[key] - returns the name for that key
myobject[value] - returns the last elements', with that name, value
meaning for field_2 it will return 6, though it'll print it 3 times, as it repeats 3 times in the object.
意味着对于 field_2 它将返回 6,尽管它会打印 3 次,因为它在对象中重复 3 次。
My question is how to obtain the correct value for that duplicate named fields and not just the last one's
我的问题是如何获取重复命名字段的正确值,而不仅仅是最后一个
Thank you
谢谢
回答by Coin_op
That is not an array that is an object. You'd be better creating a property of the object that is an array and store the different values in there.
那不是一个对象数组。您最好创建一个数组对象的属性并将不同的值存储在其中。
var myarray = {
"field_1": "lorem ipsum",
"field_array": []
};
myarray.field_array.push(value);
then just loop through that property of the array.
然后循环遍历数组的那个属性。
回答by gblazex
- Your code has invalid syntax.
- There are no assocative arrays in Javascript
- The thing you defined is an Object
- If you give value to a property 3 times, sure it will contain the last value
- 您的代码语法无效。
- Javascript 中没有关联数组
- 你定义的东西是一个对象
- 如果你给一个属性赋值 3 次,确保它包含最后一个值
Test
测试
var obj = {
"field_1": "lorem ipsum",
"field_2": 1,
"field_2": 2,
"field_2": 6
};
for ( var i in obj ) {
console.log(i + " = " + obj[i]);
}
OUTPUT
输出
field_1 = lorem ipsum
field_2 = 6
回答by kwah
Associative arrays do not exist in Javascript - what you have created is an Object using the JSON format.
关联数组在 Javascript 中不存在 - 您创建的是使用 JSON 格式的对象。
I suspect that something like this will give you more what you are seeking, though I suggest questioning exactly what it is that you are trying to achieve..
我怀疑这样的事情会给你更多你正在寻找的东西,尽管我建议你确切地质疑你正在努力实现的目标。
The following code will allow you to access multiple instances of duplicated 'keys', but is
以下代码将允许您访问重复的“密钥”的多个实例,但是
var myDataset = [
{ "field_1": "lorem ipsum" },
{ "field_2": 1 },
{ "field_2": 2 },
{ "field_2": 6 }
];
$.each(myDataset, function(valuePairIndex, value)
{
$.each(myDataset[valuePairIndex], function(key, value1)
{
var valuePair = myDataset[valuePairIndex];
console.log(valuePairIndex);
console.log(key + ' = ' + valuePair[key]);
// console.log('key = ' + key);
// console.log('valuePair[key] = ' + valuePair[key]);
});
});
回答by Spudley
You can't do this. The array key must be unique.
你不能这样做。数组键必须是唯一的。
If you've got Firefox/Firebug installed (or similar in another browser), you can try it by entering this into the Firebug console:
如果您已经安装了 Firefox/Firebug(或在其他浏览器中安装了类似工具),您可以通过在 Firebug 控制台中输入以下内容来尝试:
var myarray = {
"field_1": "lorem ipsum",
"field_2": 1,
"field_2": 2,
"field_2": 6
};
console.dir(myarray);
Firebug will respond with:
Firebug 将响应:
field_1 "lorum ipsum"
field_2 6
in other words, it works, but each subsequent value specified for field_2 overwrites the previous one; you can only have one value for it at a time.
换句话说,它可以工作,但是为 field_2 指定的每个后续值都会覆盖前一个值;一次只能有一个值。
The closest you can get to what you want is to make field_2 an array in itself, something like this:
最接近您想要的东西是使 field_2 本身成为一个数组,如下所示:
var myarray = {
"field_1": "lorem ipsum",
"field_2": [1,2,6]
};
If you do console.lognow, you'll get this:
如果你console.log现在这样做,你会得到这个:
field_1 "lorum ipsum"
field_2
0 1
1 2
2 6
Hope that helps.
希望有帮助。
回答by David M?rtensson
It is not possible.
这不可能。
The resulting object does only contain 2 elements, the first and second field_2 elements are lost on creation.
结果对象只包含 2 个元素,第一个和第二个 field_2 元素在创建时丢失。
回答by Bal
The keys must be unique.
键必须是唯一的。
回答by Alex
The only way to get around it would be to either change the fields to unique identifiers, or something like:
解决它的唯一方法是将字段更改为唯一标识符,或者类似:
var myarray = {
"field_1": "lorem ipsum",
"field_2": [
{"value_1": 1},
{"value_2": 2},
{"value_3": 6}
]
};
回答by dxh
You're overwriting the same value several times.
您多次覆盖相同的值。
What you want is probably something like:
你想要的可能是这样的:
var myarray = {
"field_1": "lorem ipsum",
"field_2": [1,2,6]
};
Which could be written in a manner similar to what you currently have:
可以以类似于您目前拥有的方式编写:
var myarray = {};
myarray.field_1 = [];
myarray.field_1.push('lorem ipsum');
myarray.field_2 = [];
myarray.field_2.push(1);
myarray.field_2.push(2);
myarray.field_2.push(6);
Note that I made field_1an array as well, which - for consistency - I thought you might want.
请注意,我也制作了field_1一个数组,为了一致性,我认为您可能需要。

