将项目添加到 JavaScript 数组

发布于 2024-09-18 15:04:27 字数 602 浏览 5 评论 0原文

我正在尝试将项目分配给用户操作的数组,例如,用户单击“添加”,这会将选定的 id 添加到数组的相关部分。

首先不会创建数组,因此我设置了一个空数组:

var Options={};

在用户单击时,我想分配一个键来匹配从下拉列表中选择的选项,以便稍后可以将其用作参考。

例如,当用户在记录上单击加号时,从下拉列表中选择的选项是 2。他们选择的记录的 id 是 5。

我想按如下方式构造我的数组:-

[key 例如 drop down选项] => '记录' => array [record_id]

每次用户单击记录旁边的加号时,id 都会根据下拉选择的选项附加到正确的数组中。

[option 1] => 'publication_date' = 12/09/2010, 'records' => [1, 2, 4]
[option 2] => 'publication_date' = 15/09/2010, 'records => [1, 3, 5]

等等,选择框中的每个选项都有一个发布日期和用户可以分配给它的一系列记录。

I'm attempting to assign items to an array on a users action, so for example, user clicks "Add", this will add the selected id into the relevant section of the array.

The array won't be created at first so I set up an empty array:

var Options={};

On the users click, I then want to assign a key to match the option selected from a drop down so I can use this as a reference later on.

E.g. When the user clicks plus on a record, the option selected from a drop-down is 2. The id of the record they selected is say 5.

I'd like to structure my array like the following:-

[key e.g drop down option]
=> 'records' => array [record_id]

Each time the user clicks plus next to a record, the id is appended to the correct array based on the drop down selected option.

[option 1] => 'publication_date' = 12/09/2010, 'records' => [1, 2, 4]
[option 2] => 'publication_date' = 15/09/2010, 'records => [1, 3, 5]

etc, each option from the select box has a publication date and a series of records the user can assign to it.

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

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

发布评论

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

评论(2

心奴独伤 2024-09-25 15:04:27

你可以这样做:

function AddItem(key, value)  {
   if(Options[key]) Options[key].push(value);
   else Options[key] = [value];
}

例如这样做:

​AddItem(2, 5);
AddItem(2, 6);

会导致 Options 为:

{ 2: [5, 6] }

您可以在这里尝试/使用它

You can do something like this:

function AddItem(key, value)  {
   if(Options[key]) Options[key].push(value);
   else Options[key] = [value];
}

For example doing this:

​AddItem(2, 5);
AddItem(2, 6);

Would result in Options being:

{ 2: [5, 6] }

You can give it a try/play with it here.

梦忆晨望 2024-09-25 15:04:27

javascript 中的对象适合存储一个键数据集而不是多个键。

IE:
var records = {'12/09/2010':[1, 2, 4], '15/09/2010':[1, 3, 5]}

获取第 15 条记录/09: 记录['15/09/2010']
为日期添加新记录:records['15/09/2010'].push(6)

如果日期发生变化,您需要执行以下操作:
记录['17/09/2010'] = 记录['15/09/2010'];删除记录['15/09/2010']

用户无法同时更改两者(日期和添加),您应该分别执行这两项更新操作。


现在,如果您计划拥有更多键,则应该使用对象数组:

var records = [
  {publication_date:'12/09/2010', records:[1, 2, 4], ..., otherProp='...'},
  ...
];

对于每个更改,在数组上循环,找到相关项并更改它。

An object in javascript is good to store a one key dataset not various keys.

ie:
var records = {'12/09/2010':[1, 2, 4], '15/09/2010':[1, 3, 5]}

To get the records for the 15/09: records['15/09/2010']
Add a new record for a date: records['15/09/2010'].push(6)

If the date change, you need to do something like:
records['17/09/2010'] = records['15/09/2010']; delete records['15/09/2010']

The user is not able to change both at the same time(date and add) and you should do both update actions separately.


Now if you plan to have more keys, you should use an array of objects:

var records = [
  {publication_date:'12/09/2010', records:[1, 2, 4], ..., otherProp='...'},
  ...
];

And for each change, loop on the array, find the relevant item and change it.

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