coolguy
coolguy

Reputation: 7954

Javascript index of for multiple values

Im having a multdimensional javascript arrays like this.

[
["9", "16", "19", "24", "29", "38"],
["9", "15", "19", "24", "29", "38"],
["10", "16", "19", "24", "29", "38"],
["9", "16", "17", "19", "24", "29", "39"],
["10", "16", "17", "19", "24", "29", "39"],
["9", "15", "21", "24", "29", "38"]

.......
.......
]

Around 40 to be exact

and im having an another array called check with the following values

 [9,10] //This is of size two for example,it may contain any number of elements BUT it only contains elements(digits) from the multidimensional array above

What i want is i need to make the multidimensional array unique based for the check array elements

1.Example if check array is [15] Then multidimensional array would be

[
    ["9", "15", "19", "24", "29", "38"],
   //All the results which contain 15

]

2.Example if check array is [15,21] Then multidimensional array would be

[
    ["9", "15", "21", "24", "29", "38"]
    //simply containing 15 AND 21.Please note previous example has only 15 in it not 21
    //I need an AND not an OR

]

I had tried the JavaScript IndexOf method BUt it is getting me an OR'ed result not AND

Thanks in Advance

Upvotes: 6

Views: 19351

Answers (4)

Terrel Jones Jr.
Terrel Jones Jr.

Reputation: 29

This should do it

var checkURL = function (url) {
  var final = false;
  if (url.indexOf('jpg') > -1) {
    final = true
  }

  if (url.indexOf('jpeg') > -1) {
    final = true
  }

  if (url.indexOf('gif') > -1) {
    final = true
  }

  if (url.indexOf('png') > -1) {
    final = true
  }

  return final
}

Upvotes: 0

Amberlamps
Amberlamps

Reputation: 40498

There is also a solution that is not using filter() or every():

// function

function AndArray(start, check) {

    this.stripArray = function(element, array) {
        var _array = [];
        for(var i = 0; i < array.length; i++)
            for(var j = 0; j < array[i].length; j++)
                if(element == array[i][j]) _array.push(array[i]);
        return _array;
        }

    for(var k = 0; k < start.length; k++)
        if(start.length > 0) start = this.stripArray(check[k], start);

    return start;

    }

// use

var check = [15, 21];

var start= [[ 15, 6 , 8], [3, 21, 56], [15, 3, 21]];

console.log(AndArray(start, check));    

Upvotes: 2

pimvdb
pimvdb

Reputation: 154958

Using .filter and .every, you can filter the rows for which every argument is apparent in the row:

var filter = function(args) {
  return arr.filter(function(row) {
    return args.every(function(arg) {
      return ~row.indexOf(String(arg));  // [15] should search for ["15"]
    });
  });
};

.every effectively does an AND operation here; for OR you could use .some instead. Those array functions are available on newer browsers.

Upvotes: 4

nnnnnn
nnnnnn

Reputation: 150080

You can use the .filter() method:

var mainArray = [ /* your array here */ ],
    check = ["15", "21"],
    result;

result = mainArray.filter(function(val) {
    for (var i = 0; i < check.length; i++)
        if (val.indexOf(check[i]) === -1)
            return false;    
    return true;
});

console.log(result);

Demo: http://jsfiddle.net/nnnnnn/Dq6YR/

Note that .filter() is not supported by IE until version 9, but you can fix that.

Upvotes: 5

Related Questions