javascript 使用 jQuery 访问关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14784051/
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
Accessing an associative array using jQuery
提问by Hardik Shah
I have an associative array here -
我这里有一个关联数组 -
var dataset = {
"person" : [
{"userLabels": ["Name","Role"]},
{"tagNames": ["lName","role"]},
{"tableClass": "width530"},
{"colWidths": ["50%","50%"]}
]
}
I tried accessing the 'userLabels' object using jQuery using various methods but I failed. I think I am doing something wrong with basics. I want the userLabels object to be accessed using jQuery and the result should be an array, so I can perform the jQuery.inArray() operation.
我尝试使用 jQuery 使用各种方法访问“userLabels”对象,但我失败了。我想我在基础知识上做错了。我希望使用 jQuery 访问 userLabels 对象并且结果应该是一个数组,因此我可以执行 jQuery.inArray() 操作。
回答by Klik
Firstly, here's how you can access dataset using the method you have.
首先,这是使用您拥有的方法访问数据集的方法。
var dataset =
{
"person" : [
{"userLabels": ["Name","Role"]},
{"tagNames": ["lName","role"]},
{"tableClass": "width530"},
{"colWidths": ["50%","50%"]}
]
};
alert(dataset['person'][0]['userLabels']); //style 1
alert(dataset.person[0]['userLabels']); //style 2
alert(dataset.person[0].userLabels); //style 3
//Also you can use variables in place of specifying the names as well i.e.
var propName ='userLabels';
alert(dataset.person[0][propName]);
//What follows is how to search if a value is in the array 'userLabels'
$.inArray('Name', dataset.person[0].userLabels);
I'd like to ask why you're doing this in a such an 'interesting way'. Why don't you just make them all objects?
我想问你为什么要以如此“有趣的方式”来做这件事。你为什么不把它们全部变成对象?
That's what I would do if I were you because if you think about it, a person IS an object and it should be noted that arrays are basically objectsin Javascript with a 'length' property, though I won't elaborate on it here (feel free to do some research though). I'm guessing it's because you don't know how to iterate over object properties. Of course if it makes more sense to you, go for it.
如果我是你,这就是我会做的事情,因为如果你考虑一下,一个人就是一个对象,应该注意的是,数组基本上是 JavaScript 中具有“长度”属性的对象,尽管我不会在这里详细说明(可以随意做一些研究)。我猜这是因为您不知道如何迭代对象属性。当然,如果它对你更有意义,那就去做吧。
Note one of the differences between an array and an object is that object properties need to be defined; you'll notice that I gave 'Name' and 'Role' values of 'undefined' below.
请注意,数组和对象之间的区别之一是需要定义对象属性;你会注意到我在下面给出了 'undefined' 的 'Name' 和 'Role' 值。
In any case, here is what I would do:
无论如何,这就是我要做的:
var dataset =
{
"person" : {
"userLabels": {"Name" : undefined,"Role": undefined},
"tagNames": {"lName" : undefined,"role" : undefined},
"tableClass": "width530",
"colWidths": ["50%","50%"]
}
};
for (var i in dataset) { //iterate over all the objects in dataset
console.log(dataset[i]); //I prefer to use console.log() to write but it's only in firefox
alert(dataset[i]); // works in IE.
}
//By using an object all you need to do is:
dataset.person.userLabels.hasOwnProperty('Role'); //returns true or false
Anyways, hope this helps.
无论如何,希望这会有所帮助。
回答by undefined
var basic = dataset.person[0].userLabels;
// | | |
// | | --- first element = target object
// | --- person property
// ---- main-object
回答by keithjgrant
var userLabels = dataset.person[0].userLabels;
if ($.inArray(yourVal, userLabels) !== -1) {
doStuff();
}