[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Arrays & Loops

Posted on 2011-10-06
13
Medium Priority
?
207 Views
Last Modified: 2012-05-12
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)?
0
Comment
Question by:Ray Turner
  • 7
  • 3
  • 3
13 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 36926649
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
 

Author Comment

by:Ray Turner
ID: 36927138
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
 
LVL 14

Expert Comment

by:quizwedge
ID: 36927154
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:zappafan2k2
ID: 36927157
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
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 36927167
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
 

Author Comment

by:Ray Turner
ID: 36928610
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
 
LVL 14

Assisted Solution

by:quizwedge
quizwedge earned 1000 total points
ID: 36928917
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
 

Author Comment

by:Ray Turner
ID: 36930152
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
 

Author Comment

by:Ray Turner
ID: 36930201
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
 

Author Comment

by:Ray Turner
ID: 36930228
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
 

Author Comment

by:Ray Turner
ID: 36930275
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
 
LVL 12

Accepted Solution

by:
zappafan2k2 earned 1000 total points
ID: 36930860
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
 

Author Closing Comment

by:Ray Turner
ID: 36931720
Thanks!!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question