0

I am new to javascript and recently have a problem to passing elements from array to var.

For example, I have an array like var anArray = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]], and I have 3 different var a , b and c.

after some loop codes,

what I would like to see is:

while a=a1, b should be b1 and c=c1

while a=a2, b=b2 and c=c2

while a=a3, b=b3, c=c3

also pls consider that what if I have array like:

[[a1,a2,a3],[b1,b2,b3]] which will result a=a1 while b=b1, a=a2 while b=b2 etc.

or [[a1,a2],[b1,b2],[c1,c2]] which will result a=a1 while b=b1 and c=c1, a=a2 while b=b2 and c=c2

If my question is still not clear enough, please comments it and I will update it.

I really appreciate all the comments and the code that you have made! Many thanks!

4
  • 9
    var anArray = {{1,2},{1,2}} is not an array, it's an invalid object. An array would look like: var anArray = [{1,2},{1,2}]. That's an array of objects. An array of arrays would be var anArray = [[1,2],[1,2]] Commented Oct 22, 2014 at 20:58
  • 1
    What is result in your code? Can you clarify why a and b would get those values? You are assigning the same value to a and b in the code, so they can't be different. Commented Oct 22, 2014 at 21:02
  • Could you explain what you want to achieve. Is it possible to have an array like [[1,2,3],[4,5,6],[7,8,9]] and what should be the output? Commented Oct 22, 2014 at 21:08
  • @Wezelkrozum based on your array, the result should be 3 vars combined together and will show 147 258 369 Commented Oct 22, 2014 at 21:18

3 Answers 3

1

You have a bunch of pieces backwards and in the wrong place:

var anArray = [[1,2],[1,2]]; 
for(var i=0;i <= anArray.length - 1;i++)
{
    for(var j=0;j<anArray[i].length;j++){
        var a = anArray[i][j];
        var b = anArray[i + 1][j];
        alert("a: "+a+" and b: "+b);
    }

}

Edit: adjusted after you changed your entire question.

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

6 Comments

i think that he wants this a = anArray[i][j];, b = anArray[j][i];
@Damon.s That would make no sense as it assumes the outer array and inner array will always have the same length, which he doesn't indicate
@Damon.s and he just changed the entire question.
@DukeWang there is no bug. notice it is less than or equal to the length minus 1
@DonRhummy sorry for that I have post this question in a hurry and didn't considered different situations. I have updated it.
|
0

Let say that we have have an array like thisvar anArray = [[1,2,3],[4,5,6]]; and if you want to alert this a=14;b=25;c=36; then you can use this code

var anArray = [[1,2,3],[4,5,6]];
for ( i = 0; i < anArray.length; i++ ) {
    var  l = anArray[i];
    for ( m = 0; m < l.length; m++ ){
        this["a"+i+m.toString()] = l[m];
    }
}
alert("a = "+ a00 + a10.toString());
alert("b = "+ a01 + a11.toString());
alert("c = "+ a02 + a12.toString());

Where a00=1; a01=2; a02=3; these are the elements of the first array.(the middle nr. tells the array so for the first array we use 0). Then we have a10=4; a11=5; a12=6; these are the elements of the 2nd array.(the middle nr. tells the array so for the second array we use 1).

All you have to do is just replace this arrayvar anArray = [[1,2,3],[4,5,6]] with yours and let javascript do its job.

1 Comment

Your answer have given me the perfect idea which is the easiest way that can solve my problems. Many appreciate!
0

After you clarified your question I got a general idea of what you want. I think this is what you want to achieve. The example is dynamic so that as long as the length of item in the array is equal.

    var anArray = [[1,2,3],[4,5,6],[7,8,9]]; 
    for(var j=0;j<anArray[0].length;j++){
        var values = [];
        for(var i=0;i<anArray.length;i++) {
            values[i] = anArray[i][j];
        }

//Do what you want with the values below. I chose to show them in a alert message

        var text = '';
        for(var i=0;i<anArray.length;i++) {
            if(text.length>0){ text += ',' };
            text += values[i];
        }
        window.alert('Values: ' + text);
    }

PS: There were not 1 but a few things wrong with your code.

5 Comments

I think my question was not clear enough... I have updated my question, any advice will be appreciated!
My answer should still be valid. You can run it now because I converted it to a code snippet.
the code is working but didn't solve my problem... Maybe I confused you in my previous response, I would like those vars can be used separately.
I still don't get what you exactly want. Do want to be able to iterate over the array in such a way you can have the values in a list so you can use them separately within the iteration?
I edited my answer so you are able to do what you want with the array of values I retrieve with the iterator. This should make you able to do anything with the values.

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.