[英]Javascript index of for multiple values
Im having a multdimensional javascript arrays like this. 我有像这样的多维javascript数组。
[
["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 大概40左右
and im having an another array called check with the following values 我正在使用另一个名为check的数组,其中包含以下值
[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 1.例如,如果检查数组是
[15]
那么多维数组就是
[
["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 2.例如,如果检查数组是
[15,21]
那么多维数组就是
[
["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 我曾经尝试过JavaScript IndexOf方法BUt它给我一个OR结果而不是AND
Thanks in Advance 提前致谢
You can use the .filter()
method : 您可以使用
.filter()
方法 :
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/ 演示: http : //jsfiddle.net/nnnnnn/Dq6YR/
Note that .filter()
is not supported by IE until version 9, but you can fix that . 请注意,在版本9之前,IE不支持
.filter()
,但您可以解决此问题 。
Using .filter
and .every
, you can filter the rows for which every argument is apparent in the row: 使用
.filter
和.every
,您可以过滤行中每个参数显而易见的行:
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; .every
有效地进行AND
操作; for OR
you could use .some
instead. 对于
OR
你可以使用.some
代替。 Those array functions are available on newer browsers. 这些数组函数可在较新的浏览器上使用。
There is also a solution that is not using filter()
or every()
: 还有一个不使用
filter()
或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 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.