如何在javascript中访问特定对象?

发布于 2024-10-14 02:33:39 字数 1370 浏览 2 评论 0原文

在这方面我有点菜鸟。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长梦不多时 2024-10-21 02:33:39

你可以这样做吗:

var colorQty = product.p20lb.one;
var myProduct = product.getPrice(144, colorQty);

getPrice: function (productQty, colorQty) {
    return productQty * colorQty;
}

Can you just do:

var colorQty = product.p20lb.one;
var myProduct = product.getPrice(144, colorQty);

getPrice: function (productQty, colorQty) {
    return productQty * colorQty;
}
梦纸 2024-10-21 02:33:39

请尝试使用 this[productName][tier]。在 JavaScript 中,点符号和方括号符号是相同的。也就是说,obj.aobj['a'] 相同。

Try this[productName][tier] instead. In JavaScript, the dot notation and bracket notation are identical. That is, obj.a is the same as obj['a'].

风月客 2024-10-21 02:33:39

简单的。

getPrice: function (productQty,colorQty,productName,tier) {

    return this[productName][tier] * productQty;

}

可以通过数组表示法来访问对象。不要忘记将其包装在 try-catch 块中以处理特定组合键不存在的情况。

Simple.

getPrice: function (productQty,colorQty,productName,tier) {

    return this[productName][tier] * productQty;

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文