我想要一个抽象方法根据属性名作为参数传递给方法来读取json对象的属性。在jQuery中获取传递给方法的属性值
我觉得比较容易解释一个例子。
假设我有以下的JSON对象:
var coll =
[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
function GetPropertyValue(collection, index, property_name)
{
:
:
}
其中
GetPropertyValue(coll, 0, 'firstName');
返回 “约翰”,而
GetPropertyValue(coll, 0, 'lastName');
返回“李四,并
GetPropertyValue(coll, 2, 'lastName');
返回 “琼斯”
问候。
'property_name'是一个“字符串”,不能在对象前面,因为它是一个特定的现有属性。我错了吗? – user3021830 2014-11-25 14:54:26
@ user3021830啊!对不起..检查我的编辑出 – 2014-11-25 14:55:44
这是完美的。非常感谢你。 – user3021830 2014-11-25 15:06:39