Results 1 to 3 of 3

Thread: Searching Multidimensional Arrays by String Match and Returning Elements in Row

  1. #1
    Join Date
    Feb 2003

    Question Searching Multidimensional Arrays by String Match and Returning Elements in Row

    Hi all,

    I need to be able to match a particular element in a "row" of a multidimensional array, and then find and reference the other elements in that "row".

    (Sorry, I am new-ish to programming and JS and don't know the proper terminology. Perhaps this is why I couldn't find any results when searching for this issue!)

    Below is a sample of the whole array...
    var commercialProductList=new Array(
    new Array("Sydney Automobiles - Online","Sydney_Automobiles_Online","users",60.39,3.02,1.21),
    new Array("Sydney Automobiles - Hard Copy","Sydney_Automobiles_HardCopy","copies",54.67,14.59),
    new Array("Sydney Automobiles - Hard Copy (w Online)","Sydney_Automobiles_HardcopyOnline","copies",14.59,14.59),
    new Array("Sydney Motorboats","Sydney_Motorboats","users"),
    new Array("North Sydney Automobiles - Online","NorthSydney_Automobiles_Online","users",48.40,2.42,0.97),
    new Array("North Sydney Automobiles - Hard Copy","NorthSydney_Automobiles_HardCopy","copies",43.54,14.59),
    new Array("North Sydney Automobiles - Hard Copy (w Online)","NorthSydney_Automobiles_HardcopyOnline","copies",14.59,14.59),
    new Array("North Sydney Motorboats","NorthSydney_Motorboats","users",50.50,4.25,2.15),
    Note: All rows in the array will have 3 string entries as in the sample above. The number of numeric (price) entries following will vary from row to row, some with only two prices, some with three (see example above).

    I'm wanting to search the 2nd element of each array row and comparing to a var called myvar.

    So for example, if I was searching the example above with myvar set = "NorthSydney_Automobiles_Online", I would get the following items...

    "North Sydney Automobiles - Online"

    ...and be able to reference these somehow, like commercialProductList[5][1], commercialProductList[5][2], commercialProductList[5][3], etc etc etc. Maybe numbers aren't the best reference in this case?

    Any help here is VERY much appreciated!



    Officially confused.

  2. #2
    Join Date
    Feb 2006
    A couple points-
    Don't leave a trailing comma after the last array item.
    You don't need to use the new Array() constructor, an array literal is simpler and works at least as well.

    var commercialProductList= [
        ["Sydney Automobiles - Online", "Sydney_Automobiles_Online", "users", 60.39, 3.02, 1.21],
        ["Sydney Automobiles - Hard Copy", "Sydney_Automobiles_HardCopy", "copies", 54.67, 14.59],
        ["Sydney Automobiles - Hard Copy (w Online)", "Sydney_Automobiles_HardcopyOnline", "copies", 14.59, 14.59],
        ["Sydney Motorboats", "Sydney_Motorboats", "users"],
        ["North Sydney Automobiles - Online", "NorthSydney_Automobiles_Online", "users", 48.40, 2.42, 0.97],
        ["North Sydney Automobiles - Hard Copy", "NorthSydney_Automobiles_HardCopy", "copies", 43.54, 14.59],
        ["North Sydney Automobiles - Hard Copy (w Online)", "NorthSydney_Automobiles_HardcopyOnline", "copies", 14.59, 14.59],
        ["North Sydney Motorboats", "NorthSydney_Motorboats", "users", 50.50, 4.25, 2.15]
    function searchCPL(what, find){
        for(var i= 0, L= what.length; i<L; i++){
            if(what[i][1]=== find) return what[i];
        return '';
    searchCPL(commercialProductList, "Sydney_Automobiles_HardcopyOnline");
    /*  returned value: (Array)
    Sydney Automobiles - Hard Copy (w Online),Sydney_Automobiles_HardcopyOnline,copies,14.59,14.59

  3. #3
    Join Date
    Feb 2003
    That's bloody brilliant MrHoo, thanks very much!!!
    Officially confused.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center