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
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
Share Improve this question edited Aug 9, 2012 at 11:04 coolguy asked Aug 9, 2012 at 10:56 coolguycoolguy 7,9549 gold badges47 silver badges72 bronze badges 9- 3 "Around 40 to be exact" - Ah, irony. – nnnnnn Commented Aug 9, 2012 at 10:58
- @nnnnnn ..hope you got the idea.if not ill explain more – coolguy 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 ment was just poking fun at your choice of words there.) – nnnnnn Commented Aug 9, 2012 at 11:01
-
Yes ...@nnnnnn The keyword is i needed an
AND
ed result :) – coolguy Commented Aug 9, 2012 at 11:02 - 1 @nnnnnn: That is probably one of his exactly around 40 ones :) – Amberlamps Commented Aug 9, 2012 at 11:03
4 Answers
Reset to default 5You 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/nnnnnn/Dq6YR/
Note that .filter()
is not supported by IE until version 9, but you can fix that.
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.
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));
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
}