如何在javascript中访问特定对象?
在这方面我有点菜鸟。我正在使用 JSON 定义一个名为 product
的类/对象,但我不确定如何传递参数来访问该对象的特定部分。请原谅我无法更好地解释它。希望我的代码示例能有所帮助:
var product = {
p14lb: {
one: 10.00,
two: 20.00,
three: 30.00,
four: 40.00,
five: 50.00,
six: 60.00,
colorCharge: 2.00
},
p20lb: {
one: 20.00,
two: 30.00,
three: 40.00,
four: 50.00,
five: 60.00,
six: 70.00,
colorCharge: 1.00
},
getPrice: function (productQty,colorQty) {
// I can get the values like this
return this.p20lb.one * productQty;
// but i'd like to be able to pass in the value of what object I want to access
// so instead of hard coding p20lb.one, 'p20lb' would be an argument I could pass
// to getPrice, and 'one' would also be an argument
}
};
这就是我当前访问它的方式,效果很好。
var myProduct = product.getPrice(144,2);
正如我在上面代码示例的注释中所说,我想不硬编码 p20lb.one,“p20lb”将是我可以传递给 getPrice 的参数,“one”也将是成为一个论据。所以也许是这样的:
getPrice: function (productQty,colorQty,productName,tier) {
return this.productName.tier * productQty;
}
var myProduct = product.getPrice(144,2,'14lb','two');
但这不行,无论我做什么,我都会得到未定义
。在这方面我显然是个菜鸟。我想做的事情可能吗?如果是这样,你能指出我正确的方向吗?谢谢!
I am a bit of a noob when it comes to this. I am using JSON to define a class/object called product
and I am not sure how to pass arguments to access a certain part of that object. Pardon me for not being able to explain it better. Hopefully my code sample will help:
var product = {
p14lb: {
one: 10.00,
two: 20.00,
three: 30.00,
four: 40.00,
five: 50.00,
six: 60.00,
colorCharge: 2.00
},
p20lb: {
one: 20.00,
two: 30.00,
three: 40.00,
four: 50.00,
five: 60.00,
six: 70.00,
colorCharge: 1.00
},
getPrice: function (productQty,colorQty) {
// I can get the values like this
return this.p20lb.one * productQty;
// but i'd like to be able to pass in the value of what object I want to access
// so instead of hard coding p20lb.one, 'p20lb' would be an argument I could pass
// to getPrice, and 'one' would also be an argument
}
};
This is how I am accessing it currently, works great.
var myProduct = product.getPrice(144,2);
As I said in the comment in the above code sample, I'd like to not hard code p20lb.one, 'p20lb' would be an argument I could pass to getPrice, and 'one' would also be an argument. So maybe something like this:
getPrice: function (productQty,colorQty,productName,tier) {
return this.productName.tier * productQty;
}
var myProduct = product.getPrice(144,2,'14lb','two');
But that is no go, I get undefined
no matter what I do. I'm clearly a noob when it comes to this. Is what I'm trying to do possible? If so, can you point me in the right direction? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做吗:
Can you just do:
请尝试使用
this[productName][tier]
。在 JavaScript 中,点符号和方括号符号是相同的。也就是说,obj.a
与obj['a']
相同。Try
this[productName][tier]
instead. In JavaScript, the dot notation and bracket notation are identical. That is,obj.a
is the same asobj['a']
.简单的。
可以通过数组表示法来访问对象。不要忘记将其包装在 try-catch 块中以处理特定组合键不存在的情况。
Simple.
Objects can be accessed in array notation. Don't forget to wrap it in a try-catch block to handle the cases where a certain key combination does not exist.