Search multi-dimensional array JavaScript

I have an array which looks like this :

selected_products[0]=["r1","7up",61,"Albertsons"]
selected_products[1]=["r3", "Arrowhead",78,"Arrowhead "]
selected_products[2]=["r8", "Betty Crocker Cake Mix (Variety)",109,"Arrowhead "]
...

how can I search for an item in this array according to the first entry in each item (r1,r2,..) the array is huge I am looking for a fast an effective way to get results from this array I used the JQuery function jQuery.inArray but it couldn't find any thing in my array , I used it this way :

alert($.inArray(["r1","7up",61,"Albertsons"],selected_products))// it returns -1
alert($.inArray("r1",selected_products))//this also returns -1

Answers:

Answer

If you want it to be fast, you'll want a for loop so that you can break the loop when the match is found.

var result;
for( var i = 0, len = selected_products.length; i < len; i++ ) {
    if( selected_products[i][0] === 'r1' ) {
        result = selected_products[i];
        break;
    }
}

Of course this assumes there's only one match.


If there's more than one, then you could use $.grep if you want jQuery:

var result = $.grep(selected_products, function(v,i) {
    return v[0] === 'r1';
});

This will give you a new Array that is a subset of the matched items.


In a similar manner, you could use Array.prototype.filter, if you only support modern JavaScript environments.

var result = selected_products.filter(function(v,i) {
    return v[0] === 'r1';
});

One other solution would be to create an object where the keys are the rn items. This should give you a very fast lookup table.

var r_table = {};
for( var i = 0, len = selected_products.length; i < len; i++ ) {
    r_table[selected_products[i][0]] = selected_products[i];
}

Then do your lookups like this:

r_table.r4;

Again this assumes that there are no duplicate rn items.

Answer

So you are trying to find the index of the matched result?, well that changes the things a little bit:

var index=-1;

for(var i = 0, len = selected_products.length; i < len; i++){
    if(selected_products[i][0] === "r1"){
        index = i;
        break;
    }
}
if(index > -1){
    alert(selected_products[index].join(","));//"r1,7up,61,Albertsons"
}

Note: This will return the first result matched, if you want to get an array containing a list of all indexes:

var results=[];

for(var i = 0, len = selected_products.length; i < len; i++){
    if(selected_products[i][0] === "r1"){
        results.push(i);
    }
}

You can then call (for example) the last 'r1' matched like this selected_products[results[results.length-1]].join(",");

Answer

You might wanna consider not doing these things in Javascript but in a server sided language (PHP/Java/.NET). In this way you:

  • Won't have problems with browser incapabilities (Mostly IE errors)
  • Shorter Javascript code and therefore faster to load.
  • Your site also works with Javascript turned off.

An example how to do this in PHP:

<?php 
function search($array, $key, $value) 
{ 
    $results = array(); 

    if (is_array($array)) 
    { 
        if (isset($array[$key]) && $array[$key] == $value) 
            $results[] = $array; 

        foreach ($array as $subarray) 
            $results = array_merge($results, search($subarray, $key, $value)); 
    } 

    return $results; 
} 
?>
Answer

Just to pick up @am not i am's great input. Here is a live example. He solved it. I just wanted to give an example that it works. Hope it helps. This puts the values found from the selected value in 2 input fields.

http://jsfiddle.net/asle/ZZ78j/2/

$(document).ready(function () {
var liste = [];
liste[0] = ["First Name", "12345678", "[email protected]"];
liste[1] = ["Second Name", "20505050", "second.nametestdomain.no"];
liste[2] = ["", "", ""];
$("#selger").change(function () {
    var valg = ($(this).val());
    var telefon;
    var epost;
    for (var i = 0, len = liste.length; i < len; i++) {
        if (liste[i][0] === valg) {
            telefon = liste[i][1];
            epost = liste[i][2];
            $("#tel").val(telefon);
            $("#epost").val(epost);
            break;
        }
    }
});

});
Answer

Try this,

// returns the index of inner array, if val matches in any array
function findIn2dArray(arr_2d, val){
    var indexArr = $.map(arr_2d, function(arr, i) {
            if($.inArray(val, arr) != -1) {
                return 1;
            }

            return -1;
    });

    return indexArr.indexOf(1);
}


function test(){
    alert(findIn2dArray(selected_products, 'r8'));
}
Answer

You may create index object { r1: 1, r2: 2,..., < search key >: < element index >, ...} and use it for searching.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.