[英]Filter array of strings against array
I have an array of strings like this 我有这样的字符串数组
values[0]='1,3,16,5,12,43,13';
values[1]='7,1,3,16,5,23,49,26,13';
values[2]='13,1,3,16,5,23,49,26,12';
// etc.
on the other hand I have an array 另一方面,我有一个数组
filter[0]='13';
filter[1]='12';
filter[2]='3';`
I would like to find all instances of values where all my filter items are contained in the string (in this case values[0]
and values[2]
would match, values[1]
wouldn't because 12 is missing). 我想找到所有我的过滤器项都包含在字符串中的值的所有实例(在这种情况下,
values[0]
和values[2]
将匹配, values[1]
不会匹配,因为缺少12)。 I've been trying for hours and I guess it's really easy but I just can't seem to find a solution. 我已经尝试了几个小时,我想这确实很容易,但是似乎找不到解决方案。
jQuery is an option if it helps. 如果有帮助,可以选择使用jQuery。
You can use filter()
and every()
to do that 您可以使用
filter()
和every()
来做到这一点
var values = ['1,3,16,5,12,43,13', '7,1,3,16,5,23,49,26,13', '13,1,3,16,5,23,49,26,12'], filter = [13, 12, 3]; var res = values.filter(function(v) { // filter array return filter.every(function(fil) { // checking all elements are in the string return v.indexOf(fil) > -1; }); }) document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');
UPDATE: 更新:
There is chances to fail while using indexOf()
. 使用
indexOf()
时可能会失败。 For eg: 6
will match if string includes 16
. 例如: 如果string包含
16
则6
将匹配 。 So we can use regexObj.test(str)
or match()
with word boundary regex for more precision. 因此,我们可以将
regexObj.test(str)
或match()
与单词边界regex一起使用,以提高准确性。
var values = ['1,3,16,5,12,43,13', '7,1,3,16,5,23,49,26,13', '13,1,3,16,5,23,49,26,12'], filter = [13, 12, 3]; var res = values.filter(function(v) { // filter array return filter.every(function(fil) { // checking all elements are in the string return new RegExp('\\\\b' + fil + '\\\\b').test(v); // or return v.match(new RegExp('\\\\b' + fil + '\\\\b')); }); }) document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');
You can use filter and reduce where the predicate is a regexp that checks that the value matches all of the values in the filter as a whole word. 您可以使用filter并减少谓词是regexp的地方,该regexp可检查该值是否与整个单词中的过滤器中的所有值匹配。
var values = ['1,3,16,5,12,43,13', '7,1,3,16,5,23,49,26,13', '13,1,3,16,5,23,49,26,12'];
var filter = [13, 12, 3];
var filteredValues = values.filter(function(value){
return filter.reduce(function(p,c){
return p && new RegExp('\\b'+c+'\\b').test(value)
})
});
var values = [ '1,3,16,5,12,43,13', '7,1,3,16,5,23,49,26,13', '13,1,3,16,5,23,49,26,12' ] var filter = ['13', '12', '3'] var filteredValues = values.map(function(str) { return str.split(',') }).filter(function(nums) { return filter.every(function(n) { return ~nums.indexOf(n) }) }).map(function(nums) { return nums.join(',') }) console.log(filteredValues.join('\\n'))
Here is one way of doing that: 这是一种实现方法:
var matches = values.filter(function(str) {
return filter.every(function(item) {
return str.split(',').indexOf(item) >= 0;
});
});
This should help : 这应该有帮助:
function findMatchedValues() {
var count;
var output_array = [];
$.each(values, function(v_index, v_val) {
count = 0;
$.each(filters, function(f_index, f_val) {
if ($.inArray(f_val, v_val.split(",")) !== -1) {
++count;
}
});
if (count === filters.length) {
output_array.push(v_val);
}
});
return output_array;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.