My output should be like this: (eg. data)
myArray = [ [otherArray0], [otherArray1], [otherArrayN] ]
My try:
var myArray = [];
var num = 50;
for (i=0; i<num;i++)
{
var a = 0;
$('#divID .class select[value!=""]').each(function() //get values from a select
{
var otherArray+i = []; //each for cycle is a new "sub array"
otherArray+i[a] = $(this).val(); //store in array the values from select
a++
})
}
Am I thinking well?
keeg
3,9589 gold badges51 silver badges101 bronze badges
asked Jan 31, 2013 at 17:05
noneJavaScript
8453 gold badges22 silver badges41 bronze badges
4 Answers 4
Firstly
otherArray[i] = []; // create a new array
Then
otherArray[i][a] = $(this).val();
But your code can be made a lot simpler
var myArray = [];
var num = 50;
for (i=0; i<num;i++)
{
myArray.push($.map($('#divID .class select[value!=""]'),function(e){
return e.val();
}));
}
answered Jan 31, 2013 at 17:08
Jamiec
136k15 gold badges143 silver badges202 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
noneJavaScript
could it be
otherArray+i[i][a] = $(this).val(); ? That +i its just to create diferent arraysJamiec
@skdnewbie =
otherArray+i makes no sense and is a syntax error as you cant add an integer and ArraynoneJavaScript
look at this:
otherArray1[1,2,3,4], otherArray2[6,7,8,9] and final should be myArray[[otherArray1],[otherArray2]]A pattern of setting up an empty array ([]) then using push to fill it as you go, then pushing the array onto your final array should take you far.
var myArray = [];
for (int i=0; i<50; i++)
{
var otherArray = [];
$(/*...*/).each(function()
{
var val = /*...*/;
otherArray.push(val);
});
myArray.push(otherArray);
}
answered Jan 31, 2013 at 17:12
Plynx
11.5k3 gold badges34 silver badges34 bronze badges
3 Comments
noneJavaScript
Hum, Ill try this! Looks like what I want
noneJavaScript
The output should be:
myArray[[otherArray1],[otherArray2],[otherArrayN]]where each otherArray has diferent values. Your code will do it ?Plynx
@skdnewbie Yes. It produces an array of arrays, i.e.
[ [], [], [], [] ]Reading between lots of lines, I think this is what you want:
var myArray = [];
$("#divID .class select").each(function() {
var subarray = [];
$("option[value!='']", $(this)).each(function() {
subarray.push($(this).val());
});
myArray.push(subarray);
}
myArray will then contain:
[[sel1Opt1, sel1Opt2, sel1Opt3, ...], [sel2Opt1, sel2Opt2, sel3Opt3, ...], ...]
answered Jan 31, 2013 at 17:43
Barmar
789k57 gold badges555 silver badges669 bronze badges
Comments
var num = 50;
for (i=0; i<num;i++)
{
var myArray[i] = array ();
var a = 0;
$('#divID .class select[value!=""]').each(function() //get values from a select
{
myArray[i].push( $(this).val()); //store in array the values from select
})
}
answered Jan 31, 2013 at 17:12
luckystars
1,75412 silver badges12 bronze badges
Comments
lang-js
var myArray[i] = array ();should bevar myArray[i] = [];myArray[[otherArray1],[otherArray2],[otherArrayN]]where each otherArray has diferent values stored.[["otherArray1"],["otherArray2"], ...]?