动态创建JS对象

发布于 2024-12-01 15:40:30 字数 851 浏览 0 评论 0原文

我正在用硬编码值重写一个项目,使其变得灵活和动态。然而,我总是遇到麻烦。

我现在编写了大部分 JS 代码,但是我发现了一些无法修复的错误,所以我决定从头开始。我现在正在考虑将我的数据放入一个跟踪总数的对象中。该项目基本上让人们为每个活动订购门票、酒店等。

然而,正如我所说,这曾经是硬编码的。现在它必须灵活,我不确定是否可以动态创建对象?这是我的问题:

我曾经使用的对象如下:

var total = {   amount: 0,
            tickets: 0,
            passes: 0,
            fri: 0,
            sat: 0,
            sun: 0,
            freecarpass: 0,
            freepass_fri: 0,
            freepass_sat: 0,
            freepass_sun: 0,
            passes_fri: 0,
            passes_sat: 0,
            passes_sun: 0,
            passes_totalPurchase: 0
        };

如您所见,日期是硬编码的。然而,在新系统中,每个事件都会有不同的日期,所以我需要能够基于此构建我的对象,如果这有意义的话。

例如,如果事件从星期三到星期六运行,则对象中的日期将是:

wed: 0, thu: 0, fri: 0, sat: 0 

等等。

我不知道如何实现这一目标,如果这可能的话?如果需要,我可以提供更多项目代码。提前致谢。

I'm rewriting a project with hardcoded values to be flexible and dynamic. However, I keep running into trouble.

I wrote most of my JS code now, but I found a couple of bugs that I can't fix, so I decided to start from scratch. I was now thinking to put my data into an object which keeps track of totals. The project basically lets people order tickets, hotels, etc. per event.

However, this used to be hardcoded, like I said. Now it has to be flexible and I'm not sure if it's possible to dynamically create an object? Here's my problem:

The object I used to use was the following:

var total = {   amount: 0,
            tickets: 0,
            passes: 0,
            fri: 0,
            sat: 0,
            sun: 0,
            freecarpass: 0,
            freepass_fri: 0,
            freepass_sat: 0,
            freepass_sun: 0,
            passes_fri: 0,
            passes_sat: 0,
            passes_sun: 0,
            passes_totalPurchase: 0
        };

As you can see, the days are hardcoded. However, with the new system, every event will have different days, so I need to be able to build my object based on that, if that makes sense.

For example, if the event runs from wednesday till saturday, the days in the object would be:

wed: 0, thu: 0, fri: 0, sat: 0 

etc.

I'm not sure how to achieve this, if this is even possible? I can provide more code of the project if needed. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泅渡 2024-12-08 15:40:30

您仍然可以动态添加新属性:

total.wed = 0;

或者

total["wed"] = 0;

You ca still add new properties on the fly:

total.wed = 0;

or

total["wed"] = 0;
爱格式化 2024-12-08 15:40:30

虽然我不相信这是解决问题的最佳方法,但您可以在 Javascript 中随心所欲地添加或删除对象的属性。本质上,JS 对象只是属性的键值对。因此,您可以从拥有一个全天共享的对象开始,如下所示:

var total = {  
    amount: 0,
    tickets: 0,
    passes: 0
};

然后您可以通过为其分配一个值来添加一个属性,例如:

total.sat = 3;
total.sun = 1;

W3Schools对此有更长的解释 此处。

While I'm not convinced this is the best way to go about solving your problem, yes you can add or remove properties of an object at whim in Javascript. Essentially JS objects are just key-value pairs of properties. So you could start by having an object that all days will share, something like this:

var total = {  
    amount: 0,
    tickets: 0,
    passes: 0
};

Then you can add a property simply by assigning it a value, such as:

total.sat = 3;
total.sun = 1;

W3Schools has a longer explanation of this here.

左岸枫 2024-12-08 15:40:30

是的,在 javascript 中你可以用对象做很多事情。

要向对象添加新属性,只需执行 total["nameofyourday"]=0;

这样,如果您想动态更改“nameofyourday”的值,您可以在括号之间放置一个包含字符串的变量。就像这样:

var day="wed";
total[day]=0;

它将导致 Total.wed 值等于 0。

我认为,为了提供更具体的实现,需要更多代码。

Yes, in javascript you can do a lot of stuff with objects.

To add new property to an object just do total["nameofyourday"]=0;

This way if you want to dynamically change the value of "nameofyourday", you can put a variable containing a string between the brackets. Like that:

var day="wed";
total[day]=0;

and it will result in having a total.wed value equal to 0.

To provide a more specific implementation more code would be needed I think.

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