robert
robert

Reputation: 8717

search for array inside an array

I need to search for the presence of an array inside an array. It is similar to jQuery.inArray function.

For

var a = [ [1,2], [1,3] ];
console.log(  jQuery.inArray([1,3],a  ) )

I get -1 as ans. How to do this?

Thank you

Upvotes: 1

Views: 290

Answers (3)

fedeghe
fedeghe

Reputation: 1318

try this

function look4arr(arr, v) {
    for (var i = 0, l = arr.length; i < l; i += 1) {
        if (arr[i].toString() === v.toString()) { //or use +''
            return true;
        }
    }
    return false;
}
var a = [[1,2], 2],
    ok = [1,2],
    ko = [2,3]
look4arr(a, ok); // true
look4arr(a, ko); // false
// as far as the array you are looking at contains primitives seem to work fine

if you need to search for something "more" ...I mean object literals, regexp, functions You could use a function similar to the following one

function look4x(arr, v) {
    for (var i = 0, isObjOrArray = false, l = arr.length; i < l; i += 1) {
        isObjOrArray = {}.toString.call(arr[i]).match(/\[object\s(Array|Object)\]/);
        if (
            (isObjOrArray && JSON.stringify(arr[i])+'' == JSON.stringify(v)+'' )
            ||
            (!isObjOrArray && arr[i].toString() === v.toString())
        ) {
            return true;//or i if a jQuery.inArray output is needed 
        }
    }
    return false; // or -1 ... if ... jQuery.inArray
}
var a = [
        [1,[1,[1,[1,2]]]],
        2,
        true,
        'hei',
        Infinity,
        {"o" : "s", 'd':[1,2,3]},
        new RegExp(/\s/),
        function(){alert('hei');}
    ],
    ok = [1,[1,[1,[1,2]]]];

alert(
    look4x(a, [1,[1,[1,[1,2]]]])
    &&
    look4x(a, true)
    &&
    look4x(a, 'hei')
    &&
    look4x(a, Infinity)
    &&
    look4x(a, {"o" : "s", 'd':[1,2,3]})
    &&
    look4x(a, new RegExp(/\s/))
    &&
    look4x(a, function(){alert('hei');})
); // true, in this case

Please note that I didn`t tested it yet with a complete test; I'll post a test asap

Event if I seem to be late, hope it can help someone.

Bye

Upvotes: 0

TheBrain
TheBrain

Reputation: 5608

function inArray (needle, haystack) {
   for (var idx in haystack) { 
        if (haystack[idx].join(',') == needle.join(',')) 
          return idx; 
    }
    return -1;
}

Upvotes: 1

pimvdb
pimvdb

Reputation: 154818

In V8 (that is, Chrome), there is a nifty trick: while == does not work for arrays, <= && >= does.

You can iterate and check for each item if it's appearent:

for(var i = 0; i < a.length; i++) {
    if(a[i] >= [1, 3] && a[i] <= [1, 3]) alert(i);
}

For other browsers, you'd need a function that checks for array equality:

http://www.svendtofte.com/code/usefull_prototypes/prototypes.js

Array.prototype.compareArrays = function(arr) {
    if (this.length != arr.length) return false;
    for (var i = 0; i < arr.length; i++) {
        if (this[i].compareArrays) { //likely nested array
            if (!this[i].compareArrays(arr[i])) return false;
            else continue;
        }
        if (this[i] != arr[i]) return false;
    }
    return true;
}

Then:

for(var i = 0; i < a.length; i++) {
    if(a[i].compareArrays([1, 3])) alert(i);
}

Upvotes: 2

Related Questions