+3 votes
asked in java script by (2.6k points)


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={};

1 Answer

0 votes
answered by (2.3k points)

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.