6

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

9
  • 3
    "Around 40 to be exact" - Ah, irony. Commented Aug 9, 2012 at 10:58
  • @nnnnnn ..hope you got the idea.if not ill explain more Commented Aug 9, 2012 at 10:59
  • 1
    Yes, you want to filter the original array to return all the child arrays that contain all of the elements in the check array. Except that your second example returns a child array not shown in your original, so maybe I don't understand. (My previous comment was just poking fun at your choice of words there.) Commented Aug 9, 2012 at 11:01
  • Yes ...@nnnnnn The keyword is i needed an AND ed result :) Commented Aug 9, 2012 at 11:02
  • 1
    @nnnnnn: That is probably one of his exactly around 40 ones :) Commented Aug 9, 2012 at 11:03

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

what is up with that tilde (~) there? :/ ~row
2

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));    

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.