user2542428
user2542428

Reputation:

Storing values in array and retrieving from array when checkbox is checked

I am storing some values in array like this.
var test = []; I am pushing the values to this array as test.push(sample); I have some logic for calculating this value var sample= (a/b.length)*100; When I click on a button, the above logic for calculating value of sample is done and once I got the value of sample I am pushing it to test[] array. Now I want to retrieve all the values from the test[] array whenever I check the checkbox. I am able to do all this but I am facing a problem here. only the last pushed value is saving. but I want to save all the values that are being pushed. can anyone please help me in solving this issue.

Quick response is needed and appreciated
Regards
Hema

Upvotes: 0

Views: 77

Answers (3)

Oskar Andersson
Oskar Andersson

Reputation: 61

Your question is not perfectly clear to me, however I can at least provide a small jsFiddle that proves to you that (how) array.push works.

Other answers indicate that what you want is either a two dimensional array, or a "hashmap" or "associative array" where the array values are stored using a key name. The code here can be used in the fiddle to achieve either or...

http://jsfiddle.net/xN3uL/1/

// First if you need 2 dimensional arrays:
myArray.push( ["Orange", "Apple"] );
myArray.push( ["Mango", "Pineapple"] );

// Secondly, if you need hashmap or associative array:
var myObj = {};
myObj['key'] = 'value';
alert(myObject.key);

Upvotes: 0

user3462934
user3462934

Reputation:

You need to use 2 dimensional array for this. Use var test= new Array(); then assign value test['someKey']=sample;

or test.push(sample); . you can retrieve array value like alert(test[0]) or by iterating array with $.each(test,function(index,value){alert(value)});

Upvotes: 1

Ofer Haber
Ofer Haber

Reputation: 628

What you want to do is create an Array which would function as a list of value sets.

You would then be able to push all the changes into an array, and put it in the list.

for instance:

var mainList = new Array();

var changeListA = new Array();
var changeListB = new Array();

// do some stuff on change list **a** .. push(something)
changeListA .push(something);
changeListA .push(something);
changeListA .push(something);

// do some stuff on change list **b** .. push(something)
changeListB .push(changeListB);

mainList.push(changeListA);

Upvotes: 0

Related Questions