0

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?

6
  • 1
    var myArray[i] = array (); should be var myArray[i] = []; Commented Jan 31, 2013 at 17:07
  • @AdamRackis, ty, ive updated Commented Jan 31, 2013 at 17:08
  • It's really unclear what you're trying to get. You don't put the names of arrays in other arrays, you put the arrays themselves. What do you want the resulting data to look like? Commented Jan 31, 2013 at 17:19
  • @Barmar, The output should be: myArray[[otherArray1],[otherArray2],[otherArrayN]]where each otherArray has diferent values stored. Commented Jan 31, 2013 at 17:30
  • That's not possible output. Do you mean [["otherArray1"],["otherArray2"], ...]? Commented Jan 31, 2013 at 17:32

4 Answers 4

7

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();
    }));
}
Sign up to request clarification or add additional context in comments.

3 Comments

could it be otherArray+i[i][a] = $(this).val(); ? That +i its just to create diferent arrays
@skdnewbie = otherArray+i makes no sense and is a syntax error as you cant add an integer and Array
look at this: otherArray1[1,2,3,4], otherArray2[6,7,8,9] and final should be myArray[[otherArray1],[otherArray2]]
2

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

3 Comments

Hum, Ill try this! Looks like what I want
The output should be: myArray[[otherArray1],[otherArray2],[otherArrayN]]where each otherArray has diferent values. Your code will do it ?
@skdnewbie Yes. It produces an array of arrays, i.e. [ [], [], [], [] ]
1

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, ...], ...]

Comments

0
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

})
}

Comments

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.