0

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

2
  • Only if you share code, people can help finding the issue in that. Commented Apr 8, 2014 at 7:21
  • All pushed values are being saved, that is how array works, unless you are clearing it every time. So, unless you share code, not much we can do here. Commented Apr 8, 2014 at 7:22

3 Answers 3

1

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)});

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

Comments

0

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);

Comments

0

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);

Comments

Your Answer

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