Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 821
Evan Lévesque Avatar asked Jan 10 '12 19:01

Evan Lévesque


People also ask

How do you search for an element in a two-dimensional array?

Linear Search in 2D Array: It is used to find whether a particular element is present in the array or not by traversing every element in the array. While searching in the 2D array is exactly the same but here all the cells need to be traversed In this way, any element is searched in a 2D array.

How do you search an array in JavaScript?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

How do I access nested array?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };


2 Answers

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.

like image 61
2 revsuser1106925 Avatar answered Nov 10 '22 03:11

2 revsuser1106925


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(",");

like image 3
ajax333221 Avatar answered Nov 10 '22 03:11

ajax333221