1

I've been trying to create an array from a for loop, using push which creates an array in the format ["value, value, value, value, value, value"] but I need it to create an array in the following format: [["value, value, value"],["value, value, value"]

The original array is created without a for loop like this:

new array (["1","2",3"],["1,2,3"],["1,2,3"],["1,2,3"]); 

so how do I go about creating the same using a loop instead?

var colour = ["red","green","blue","orange"];

for (i=1; i<5; i++){

var name = $("#name"+i).val();                                      

var can = $("#candidate"+i).val();

arrayOfData = new Array([can,name,colour[i]]); 

}   
1
  • How exactly does this transformation work? Each set of 3 elements are replaced by a list element containing them? And what exactly have you tried? Commented Oct 21, 2012 at 18:58

1 Answer 1

1

Just push your arrayOfData itself to an array.

var array = [];
var colour = ["red","green","blue","orange"];
for (i=1; i<5; i++){
    var name = $("#name"+i).val();                                      
    var can = $("#candidate"+i).val();
    arrayOfData = [can,name,colour[i-1]]; 
    array.push(arrayOfData);
}   

​ Demo here: http://jsfiddle.net/f5J5z/4/

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

1 Comment

@NickSmith is this the result you need? [["1","2","red"],["1","2","3"],["1","2","3"],["1","2","orange"]);?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.