0

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.

2
  • 4
    That's not an array, it's an object. Commented Sep 6, 2010 at 14:53
  • Either way will do, I'm just after a way of holding data on before the form is submitted. Commented Sep 6, 2010 at 14:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Using the object technique, am I able to pass this to a php script for processing?
This seems to work for adding an array within an object key but what happens if at the same time as a value, I also want to a date which is key i.e. 2 specific.
@user275074 - You can serialize it to a json array for posting, e.g. JSON.stringify, take a look here for that. I'm not following the second comment, the key has to be a property, it can't be a date object...it can be a string representation of a date though.
It's difficult to explain what I'm after representing here but if I try to provide some data structure in the original post.
@user275074 - I think this is what you're after: jsfiddle.net/nick_craver/49Xs3/2 take a look at the console for structure. It'll result in { 1: { date: '12/09/2010', results: [1,2,4] }, 2: { date: '15/09/2010', results: [1,3,5] }}
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.