Javascript Arrays & Loops

I Array A:

var Colors = new Array();
Colors[0]="Red";
Colors[1]="Green";
Colors[2]="Blue";

and Array B
var Hues = new Array();
Hues[0]=["Red"];
Hues[1]=["Green"];
Hues[2]=["Blue"];


How do I programmatically loop through Colors(A) to create the array Hues(B)?
Ray TurnerSenior ConsultantAsked:
Who is Participating?
 
zappafan2k2Commented:
How about this?
var Cars = new Array();
Cars[0] = "Gallery/Car/Acura.jpg";
Cars[1] = "Gallery/Car/Honda.jpg";
Cars[2] = "Gallery/Car/Mazda.jpg";

var o = new Array();
for (x in Cars)
{
    o.push([Cars[x]]);
}

Open in new window


I ran this in Firebug and got this output:
[["Gallery/Car/Acura.jpg"], ["Gallery/Car/Honda.jpg"], ["Gallery/Car/Mazda.jpg"]]

Open in new window


That's what you're looking for, right?
0
 
quizwedgeCommented:
I think this is what you are asking for

<script type="text/javascript">

var Colors = new Array();
Colors[0]="Red";
Colors[1]="Green";
Colors[2]="Blue";

var Hues = new Array();

for (x in Colors)
{
Hues[x] = Colors[x];
}

</script>
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Not quite

I have an array that looks like this
var Cars= new Array();
Cars[0]="Gallery/Car/Honda.png";
Cars[1]="Gallery/Car/Ford.jpg";
Cars[2]="Gallery/Car/SAAB.jpg";

The function I am using has a parameter that needs an array passed to it that looks like this:
[["Gallery/Car/Honda.png"], ["Gallery/Car/Ford.jpg"], ["Gallery/Car/SAAB.jpg"]]

How do I build this value programmatically?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
quizwedgeCommented:
So, in my example above if I changed it to be a function that accepted the colors array as a parameter it'd work? Could you post the function you are trying to use?
0
 
zappafan2k2Commented:
What you originally asked for might look like this:
<script type="text/javascript">
var Colors = ["Red",'Green","Blue"];
var Hues = new Array();
for (x in Colors)
{
    var myColor = Colors[x];
    Hues[x] = [myColor];    // an array with one element
}
</script>

Open in new window


0
 
zappafan2k2Commented:
Oops, there's a single quote that should be a double in line 2
var Colors = ["Red","Green","Blue"];

Open in new window


Sorry.
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Here's a simple explanation of what I'm trying to do.  I need to convert a single dimension array to a multi-dimension array.  Here what I have but it doesn't work.  It only displays the 1st image.  How do I make it work?

            var Cars= new Array();
            Cars[0] = "Gallery/Car/Acura.jpg";
            Cars[1] = "Gallery/Car/Honda.jpg";

            var o = new Array(Cars,Cars);

            jQuery.slimbox(o, 0, { loop: true });
0
 
quizwedgeCommented:
Looks like you have the array correct. See the first example at http://www.irt.org/script/365.htm which creates a three dimensional array. For a 2 dimensional array you would use the following line

var x = new Array(new Array(1,2,3),new Array('A','B','C'));

Could it be that the jQuery part is the part that isn't working? I don't know jQuery, so I'm not certain.

Look at the output from the code below and let me know if there's something that isn't right


<html>
<body>

<script type="text/javascript">
  var Cars= new Array();
            Cars[0] = "Gallery/Car/Acura.jpg";
            Cars[1] = "Gallery/Car/Honda.jpg";

    var o = new Array(Cars,Cars);

for (i=0;i < Cars.length;i++)
{
    for(j=0; j < Cars.length;j++)
    {
       document.write(o[i][j] + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
     }
document.write("<br />");
}
</script>

Open in new window

0
 
Ray TurnerSenior ConsultantAuthor Commented:
Here's the output

Gallery/Car/Acura.jpg     Gallery/Car/Honda.jpg    
Gallery/Car/Acura.jpg     Gallery/Car/Honda.jpg    

For the 1st column I was expecting to see

Gallery/Car/Acura.jpg
Gallery/Car/Honda.jpg

Is that possible?
0
 
Ray TurnerSenior ConsultantAuthor Commented:
How do I create a multi-demensional array?

The result should be something like this:
Gallery/Car/Acura.jpg      'Blue'
Gallery/Car/Honda.jpg     'Red'
0
 
Ray TurnerSenior ConsultantAuthor Commented:
So to create the array I would use

var x = new Array(new Array(1,2,3),new Array('A','B','C'));

How do I populate it?
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Okay.  Here's what I need.  How can I programmatically build an array to look like this


          var Cars= new Array();
            Cars[0] = "Gallery/Car/Acura.jpg";
            Cars[1] = "Gallery/Car/Honda.jpg";
            Cars[2] = "Gallery/Car/Mazda.jpg";


I need to loop thru Cars to build the following
var o = new Array(new Array("Gallery/Car/Acura.jpg"), new Array('Gallery/Car/Honda.jpg'), new Array('Gallery/Car/Mazda.jpg'));

0
 
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.