2

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.

3
  • 2
    do you know the filters will also be a single value? Commented Nov 28, 2015 at 5:59
  • 2
    If you've been trying for hours, you must have some code to show. Commented Nov 28, 2015 at 6:01
  • Can you use underscorejs? Commented Nov 28, 2015 at 6:02

5 Answers 5

4

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

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

2 Comments

Use test to check if string satisfies regex, match is used to extract strings.
a lot of good answers around here, in the end I used this solution. All my previous, own attempts were unfortunately not producing anything usable so I did'nt bother posting any of that gibberish :)
1

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

Comments

1

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

Comments

0

Here is one way of doing that:

var matches = values.filter(function(str) {
  return filter.every(function(item) {
    return str.split(',').indexOf(item) >= 0;
  });
});

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.