Multidemensional JavaScript array

They have: 222 posts

Joined: Sep 1999

I want to have an array of URLs and their title, and have a script go through and randomly pick 5 to display. But, I'm having some trouble setting it up. Right now I'm just concered with setting up the array. I couldn't find much helpful info. on this elsewhere. Any ideas?

dk01's picture

He has: 516 posts

Joined: Mar 2002

I haven't seen anything on multidimentional arrays in js but I think this method should do for what you are looking for:
http://www.oznet.ksu.edu/agronomy/TECHSPT/tutorials/javascript/arrays.htm
Once you have the got the array set up you can just have a random number generated and use an if statement to see if the index of the array is the same as the random number. See here:
http://www.javascriptkit.com/javatutors/weighrandom.shtml
Hope this helps.
-dk

They have: 222 posts

Joined: Sep 1999

I don't have time to try it now, but that looks like it'll work. Thanks Smiling

They have: 222 posts

Joined: Sep 1999

Here's what it ended up being. The only problem is that sometimes it'll display the same link more than once.

<script language="JavaScript">
<!--

function pickRandom(range)
{
    if (Math.random)
      return Math.round(Math.random() * (range-1));
    else
    {
      var now = new Date();
      return (now.getTime() / 1000) % range;
    }
}


var blogLinks;
blogLinks = new Array;
blogLinks[1] = "Kathleen|<a href="http://kathleenie.blogspot.com/" class="bb-url">http://kathleenie.blogspot.com/</a>";
blogLinks[2] = "Michelle|<a href="http://mikao.blogspot.com" class="bb-url">http://mikao.blogspot.com</a>";
blogLinks[3] = "Heal Your Church Website|<a href="http://www.healyourchurchwebsite.com/" class="bb-url">http://www.healyourchurchwebsite.com/</a>";
blogLinks[4] = "Dann|<a href="http://doulopolis.net/" class="bb-url">http://doulopolis.net/</a>";
blogLinks[5] = "Elizabeth|<a href="http://www.livejournal.com/users/elizabethmarie/" class="bb-url">http://www.livejournal.com/users/elizabethmarie/</a>";
blogLinks[6] = "Matt|<a href="http://www.supa-fly.com/journal.html/" class="bb-url">http://www.supa-fly.com/journal.html/</a>";
blogLinks[7] = "History Church|<a href="http://historychurch.blogspot.com/" class="bb-url">http://historychurch.blogspot.com/</a>";
blogLinks[8] = "Behind the Net|<a href="http://www.behindthenet.org/" class="bb-url">http://www.behindthenet.org/</a>";
blogLinks[9] = "Cheryl|<a href="http://cherdecor.blogspot.com/" class="bb-url">http://cherdecor.blogspot.com/</a>";
blogLinks[10] = "Mere Comments|<a href="http://www.touchstonemag.com/merecomments.html" class="bb-url">http://www.touchstonemag.com/merecomments.html</a>";
blogLinks[11] = "Jason|<a href="http://www.jasonkill.com/" class="bb-url">http://www.jasonkill.com/</a>";
blogLinks[12] = "Carly|<a href="http://jeffersonair.blogspot.com/" class="bb-url">http://jeffersonair.blogspot.com/</a>";

var otherLinks
otherLinks = new Array;
otherLinks[0] = "Apex Community Church|<a href="http://www.apexcommunity.net" class="bb-url">http://www.apexcommunity.net</a>";
otherLinks[1] = "Peanuts|<a href="http://www.peanuts.com/" class="bb-url">http://www.peanuts.com/</a>";
otherLinks[2] = "User Friendly|<a href="http://www.userfriendly.org/static/" class="bb-url">http://www.userfriendly.org/static/</a>";
otherLinks[3] = "Zits|<a href="http://www.kingfeatures.com/features/comics/zits/about.htm" class="bb-url">http://www.kingfeatures.com/features/comics/zits/about.htm</a>";
otherLinks[4] = "Bill O'Reilly|<a href="http://www.worldnetdaily.com/news/archives.asp?AUTHOR_ID=11" class="bb-url">http://www.worldnetdaily.com/news/archives.asp?AUTHOR_ID=11</a>";
otherLinks[5] = "Boundless Webzine|<a href="http://www.boundless.org/" class="bb-url">http://www.boundless.org/</a>";
otherLinks[6] = "Dave Sloan|<a href="http://www.davesloan.com/" class="bb-url">http://www.davesloan.com/</a>";
otherLinks[7] = "John Leo|<a href="http://www.usnews.com/usnews/opinion/jleo.htm" class="bb-url">http://www.usnews.com/usnews/opinion/jleo.htm</a>";
otherLinks[8] = "Touchstone Magazine|<a href="http://www.touchstonemag.com/" class="bb-url">http://www.touchstonemag.com/</a>";
otherLinks[9] = "Relevant Magazine|<a href="http://www.relevantmagazine.com/" class="bb-url">http://www.relevantmagazine.com/</a>";
otherLinks[10] = "Bible Gateway|<a href="http://bible.gospelcom.net/" class="bb-url">http://bible.gospelcom.net/</a>";
otherLinks[11] = "Webmaster Forums|<a href="http://www.webmaster-forums.net/" class="bb-url">http://www.webmaster-forums.net/</a>";
otherLinks[12] = "Page Resource|<a href="http://www.pageresource.com/" class="bb-url">http://www.pageresource.com/</a>";
otherLinks[13] = "Webmonkey Reference|<a href="http://hotwired.lycos.com/webmonkey/reference/" class="bb-url">http://hotwired.lycos.com/webmonkey/reference/</a>";
otherLinks[14] = "Experts Exchange|<a href="http://www.experts-exchange.com/" class="bb-url">http://www.experts-exchange.com/</a>";
otherLinks[15] = "Launch|<a href="http://launch.yahoo.com/" class="bb-url">http://launch.yahoo.com/</a>";
otherLinks[16] = "Christian Rock Radio|<a href="http://www.christianrockradio.com" class="bb-url">http://www.christianrockradio.com</a>";
otherLinks[17] = "Homestar Runner|<a href="http://www.homestarrunner.com/" class="bb-url">http://www.homestarrunner.com/</a>";
otherLinks[18] = "Fox News|<a href="http://www.foxnews.com/" class="bb-url">http://www.foxnews.com/</a>";
otherLinks[19] = "Grassroots Music Webzine|<a href="http://www.grassrootsmusic.com/webzine/" class="bb-url">http://www.grassrootsmusic.com/webzine/</a>";
otherLinks[20] = "Half.com|<a href="http://half.ebay.com/" class="bb-url">http://half.ebay.com/</a>";
otherLinks[21] = "Joel and Krista|<a href="http://www.joelandkrista.com/" class="bb-url">http://www.joelandkrista.com/</a>";
otherLinks[22] = "Everything2|<a href="http://everything2.com/" class="bb-url">http://everything2.com/</a>";
otherLinks[23] = "Slashdot|<a href="http://slashdot.org/" class="bb-url">http://slashdot.org/</a>";


var theBlogLink = "";
var theOtherLink = "";
var randomNumber = 0;
var linkParser = "";

for(i=0; i<3; i++)
{   randomNumber = pickRandom(blogLinks.length);
linkParser = blogLinks[randomNumber].split("|");
    theBlogLink+="<a href=' "+linkParser[1]+"' class='sideLinks'>";
    theBlogLink+=linkParser[0];
    theBlogLink+="</a>";
    theBlogLink+="<br />";

}

for(i=0; i<3; i++)
{   randomNumber = pickRandom(otherLinks.length);
linkParser = otherLinks[randomNumber].split("|");
    theOtherLink+="<a href=' "+linkParser[1]+"' class='sideLinks'>";
    theOtherLink+=linkParser[0];
    theOtherLink+="</a>";
    theOtherLink+="<br />";

}

document.write("| blogSPOTTING |<br />");
document.write(theBlogLink);
document.write("<br />| otherLINKS |<br />");
document.write(theOtherLink);


//-->
&lt;/script&gt;
'

They have: 222 posts

Joined: Sep 1999

Here's an updated version that won't choose the same link twice:

&lt;script language="JavaScript"&gt;
<!--
function pickRandom(range)
{
    if (Math.random)
      return Math.round(Math.random() * (range-1)) +1;
    else
    {
      var now = new Date();
      return (now.getTime() / 1000) % range;
    }
}


var blogLinks;
blogLinks = new Array;
blogLinks[1] = "Kathleen|<a href="http://kathleenie.blogspot.com/" class="bb-url">http://kathleenie.blogspot.com/</a>";
blogLinks[2] = "Michelle|<a href="http://mikao.blogspot.com" class="bb-url">http://mikao.blogspot.com</a>";
blogLinks[3] = "Heal Your Church Website|<a href="http://www.healyourchurchwebsite.com/" class="bb-url">http://www.healyourchurchwebsite.com/</a>";
blogLinks[4] = "Dann|<a href="http://doulopolis.net/" class="bb-url">http://doulopolis.net/</a>";
blogLinks[5] = "Elizabeth|<a href="http://www.livejournal.com/users/elizabethmarie/" class="bb-url">http://www.livejournal.com/users/elizabethmarie/</a>";
blogLinks[6] = "Matt|<a href="http://www.supa-fly.com/journal.html/" class="bb-url">http://www.supa-fly.com/journal.html/</a>";
blogLinks[7] = "History Church|<a href="http://historychurch.blogspot.com/" class="bb-url">http://historychurch.blogspot.com/</a>";
blogLinks[8] = "Behind the Net|<a href="http://www.behindthenet.org/" class="bb-url">http://www.behindthenet.org/</a>";
blogLinks[9] = "Cheryl|<a href="http://cherdecor.blogspot.com/" class="bb-url">http://cherdecor.blogspot.com/</a>";
blogLinks[10] = "Mere Comments|<a href="http://www.touchstonemag.com/merecomments.html" class="bb-url">http://www.touchstonemag.com/merecomments.html</a>";
blogLinks[11] = "Jason|<a href="http://www.jasonkill.com/" class="bb-url">http://www.jasonkill.com/</a>";
blogLinks[12] = "Carly|<a href="http://jeffersonair.blogspot.com/" class="bb-url">http://jeffersonair.blogspot.com/</a>";

var otherLinks
otherLinks = new Array;
otherLinks[0] = "Apex Community Church|<a href="http://www.apexcommunity.net" class="bb-url">http://www.apexcommunity.net</a>";
otherLinks[1] = "Peanuts|<a href="http://www.peanuts.com/" class="bb-url">http://www.peanuts.com/</a>";
otherLinks[2] = "User Friendly|<a href="http://www.userfriendly.org/static/" class="bb-url">http://www.userfriendly.org/static/</a>";
otherLinks[3] = "Zits|<a href="http://www.kingfeatures.com/features/comics/zits/about.htm" class="bb-url">http://www.kingfeatures.com/features/comics/zits/about.htm</a>";
otherLinks[4] = "Bill O'Reilly|<a href="http://www.worldnetdaily.com/news/archives.asp?AUTHOR_ID=11" class="bb-url">http://www.worldnetdaily.com/news/archives.asp?AUTHOR_ID=11</a>";
otherLinks[5] = "Boundless Webzine|<a href="http://www.boundless.org/" class="bb-url">http://www.boundless.org/</a>";
otherLinks[6] = "Dave Sloan|<a href="http://www.davesloan.com/" class="bb-url">http://www.davesloan.com/</a>";
otherLinks[7] = "John Leo|<a href="http://www.usnews.com/usnews/opinion/jleo.htm" class="bb-url">http://www.usnews.com/usnews/opinion/jleo.htm</a>";
otherLinks[8] = "Touchstone Magazine|<a href="http://www.touchstonemag.com/" class="bb-url">http://www.touchstonemag.com/</a>";
otherLinks[9] = "Relevant Magazine|<a href="http://www.relevantmagazine.com/" class="bb-url">http://www.relevantmagazine.com/</a>";
otherLinks[10] = "Bible Gateway|<a href="http://bible.gospelcom.net/" class="bb-url">http://bible.gospelcom.net/</a>";
otherLinks[11] = "Webmaster Forums|<a href="http://www.webmaster-forums.net/" class="bb-url">http://www.webmaster-forums.net/</a>";
otherLinks[12] = "Page Resource|<a href="http://www.pageresource.com/" class="bb-url">http://www.pageresource.com/</a>";
otherLinks[13] = "Webmonkey Reference|<a href="http://hotwired.lycos.com/webmonkey/reference/" class="bb-url">http://hotwired.lycos.com/webmonkey/reference/</a>";
otherLinks[14] = "Experts Exchange|<a href="http://www.experts-exchange.com/" class="bb-url">http://www.experts-exchange.com/</a>";
otherLinks[15] = "Launch|<a href="http://launch.yahoo.com/" class="bb-url">http://launch.yahoo.com/</a>";
otherLinks[16] = "Christian Rock Radio|<a href="http://www.christianrockradio.com" class="bb-url">http://www.christianrockradio.com</a>";
otherLinks[17] = "Homestar Runner|<a href="http://www.homestarrunner.com/" class="bb-url">http://www.homestarrunner.com/</a>";
otherLinks[18] = "Fox News|<a href="http://www.foxnews.com/" class="bb-url">http://www.foxnews.com/</a>";
otherLinks[19] = "Grassroots Music Webzine|<a href="http://www.grassrootsmusic.com/webzine/" class="bb-url">http://www.grassrootsmusic.com/webzine/</a>";
otherLinks[20] = "Half.com|<a href="http://half.ebay.com/" class="bb-url">http://half.ebay.com/</a>";
otherLinks[21] = "Joel and Krista|<a href="http://www.joelandkrista.com/" class="bb-url">http://www.joelandkrista.com/</a>";
otherLinks[22] = "Everything2|<a href="http://everything2.com/" class="bb-url">http://everything2.com/</a>";
otherLinks[23] = "Slashdot|<a href="http://slashdot.org/" class="bb-url">http://slashdot.org/</a>";
otherLinks[24] = "Air1 Radio|<a href="http://www.air1.com" class="bb-url">http://www.air1.com</a>";


var theBlogLink = "";
var theOtherLink = "";
var theBlogLinkPicks = "";
var theOtherLinkPicks = "";
var randomNumber = 0;
var linkParser = "";

for(i=0; i<3; i++)
{   randomNumber = pickRandom(blogLinks.length -1);
            while (theBlogLinkPicks.indexOf("~" + randomNumber + "~") >= 0) {
                        randomNumber = pickRandom(blogLinks.length - 1);
               }
            theBlogLinkPicks = theBlogLinkPicks + "~" + randomNumber + "~";
linkParser = blogLinks[randomNumber].split("|");
    theBlogLink+="<a href=' "+linkParser[1]+"' class='sideLinks'>";
    theBlogLink+=linkParser[0];
    theBlogLink+="</a>";
    theBlogLink+="<br />";

}

for(i=0; i<3; i++)
{   randomNumber = pickRandom(otherLinks.length -1);
            while (theOtherLinkPicks.indexOf("~" + randomNumber + "~") >= 0) {
                        randomNumber = pickRandom(otherLinks.length - 1);
               }
            theOtherLinkPicks = theOtherLinkPicks + "~" + randomNumber + "~";
linkParser = otherLinks[randomNumber].split("|");
    theOtherLink+="<a href=' "+linkParser[1]+"' class='sideLinks'>";
    theOtherLink+=linkParser[0];
    theOtherLink+="</a>";
    theOtherLink+="<br />";

}

document.write("| blogSPOTTING |<br />");
document.write(theBlogLink);
document.write("<br />| otherLINKS |<br />");
document.write(theOtherLink);


//-->
&lt;/script&gt;
'

dk01's picture

He has: 516 posts

Joined: Mar 2002

Glad to see you got it. Looks like a good script.
-dk

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.