Link to home
Start Free TrialLog in
Avatar of RayT
RayTFlag for United States of America

asked on

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)?
Avatar of quizwedge
quizwedge
Flag of United States of America image

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>
Avatar of RayT

ASKER

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?
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?
Avatar of zappafan2k2
zappafan2k2

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


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

Open in new window


Sorry.
Avatar of RayT

ASKER

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 });
SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RayT

ASKER

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?
Avatar of RayT

ASKER

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'
Avatar of RayT

ASKER

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?
Avatar of RayT

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RayT

ASKER

Thanks!!!!