Reputation: 4258
I have this javascript code which generate a multi array with static values:
var items = [ ["red", "blue"], ["green", "yellow"] ];
console.log(items[1][1]); // green
but now I would like to fill the values dynamically. I tried this:
var items = [[]];
$.each($("input[name='checkboxColors1']:checked"), function(){
items[0].push($(this).val());
});
$.each($("input[name='checkboxColors2']:checked"), function(){
items[1].push($(this).val());
});
items[0].push... works, but items[1] not
TypeError: items[1] is undefined
Where is my fault?
Upvotes: 0
Views: 76
Reputation: 1663
Be careful when you combine static multi-dimension array, with dynamically adding elements with push.
Try the following code, that is dynamic for the two dimensions.
var checkBoxGroupNames = ['checkboxColors1', 'checkboxColors2'];
var items = [];
var checkedChkBoxes = [];
for (var i = 0; i < checkBoxGroupNames.length; i++) {
checkedChkBoxes = [];
$.each($("input[name=" + checkBoxGroupNames[i] + "]:checked"), function(){
checkedChkBoxes.push($(this).val());
});
items.push(checkedChkBoxes);
}
console.log(items); // items now holds the two dimension array
For a cleaner code, you may put the logic that find checked checkboxes per group - in a function.
var checkBoxGroupNames = ['checkboxColors1', 'checkboxColors2'];
var items = [];
$.each(checkBoxGroupNames, function(){
items.push(GetCheckedChkBoxes(this));
});
console.log(items); // items now holds the two dimension array
function GetCheckedChkBoxes(chkBoxGroupName) {
checkedChkBoxes = [];
$.each($("input[name=" + chkBoxGroupName + "]:checked"), function(){
checkedChkBoxes.push($(this).val());
});
return checkedChkBoxes;
}
Upvotes: 0
Reputation: 397
The best way to do it by creating a Array object and assign values dynamically. In this way you can also define string key as well.Please see below
var items = new Array();
items[0] = 1;
items[1] = 2;
console.log(items);
//Uncomment above block to test.
var items = new Array();
var items0 = [];
var items1 = [];
$.each($("input[name='checkboxColors1']:checked"), function(){
items0 = $(this).val();
});
$.each($("input[name='checkboxColors2']:checked"), function(){
items1 = $(this).val();
});
items[0] = items0;
items[1] = items1;
Upvotes: 0
Reputation: 251
I think you are confused about "push" function. This function insert a new element on the last position of array.
For example
var items = ["grape"];
items.push("apple");
console.log(items); //["grape", "apple"].
Upvotes: 0
Reputation: 21
You cannot push into an undefined array, you first need to create the array.
You're code has to look like this:
var items = [[], []];
$.each($("input[name='checkboxColors1']:checked"), function(){
items[0].push($(this).val());
});
$.each($("input[name='checkboxColors2']:checked"), function(){
items[1].push($(this).val());
});
Then it should work properly.
Upvotes: 1
Reputation: 17711
Javascript is asynchronous... You can't expect two functions to be executed sequentially... :-)
This way can work... But you must initialize your initial array this way:
var items = [ [], [] ];
Since with
var items = [[]];
You only define one internal array inside the external array (items[0]
), but you want two (items[0]
, items[1]
).
Upvotes: 2
Reputation: 169
var items = [[]];
You issue is here.
items[0] is an array. but items[1] is undefined.
In order to work you need to define items as [[],[]]
Or to make it more dynamic you could do check before that $.each if the items[1] exists and if not create it
Upvotes: 2