简体   繁体   中英

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

You can use filter() and every() to do that

 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() . For eg: 6 will match if string includes 16 . So we can use regexObj.test(str) or match() with word boundary regex for more precision.

 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM