views:

61

answers:

2

Hi,

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  A: 

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.

Nick Craver
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](http://www.json.org/js.html) 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.
Nick Craver
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 - Where does the date came from? You seem to have 2 keys here in your structure, what if the user changed the date and clicks add?
Nick Craver
The date comes from a date selector field that is assigned once the user has picked the records for the option they want. If the user changes the date for a specific option, then the value in the object needs to be changed.
@user275074 - I *think* this is what you're after: http://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] }}`
Nick Craver
Yes this appears to be how I'd like my end array to look but there is one problem. The publication date can be entered at any moment. I.e. the user could for instance opt to select the results for option 1 and then at some point assign a publiocation date.
@user275074 - You can split up the methods as well, like this: http://jsfiddle.net/nick_craver/49Xs3/4/
Nick Craver
+1  A: 

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.

Mic