JavaScript Filter array of strings against array filter array strings javascript,filter array strings,filter array strings

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.

Answer:1

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>');
Answer:2

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)
  })
});
Answer:3

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'))
Answer:4

Here is one way of doing that:

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

Is it possible to use behaviors to share a object between elements? <script> selectedBehavior = { properties: { selected: Object } } </script> <dom-module id="paper-...

Is it possible to use behaviors to share a object between elements? <script> selectedBehavior = { properties: { selected: Object } } </script> <dom-module id="paper-...

I want to load the left half of video texture to left Geometry and right half of video texture to the right Geometry var video = document.createElement("video"); var texture = new THREE.Texture(video)...

I want to load the left half of video texture to left Geometry and right half of video texture to the right Geometry var video = document.createElement("video"); var texture = new THREE.Texture(video)...

  1. three js copy texture

my question is really simple. It is possible use Angular Controllers with prototype? 'use strict'; var EventController = function(scope, EventModel) { this.scope = scope; this....

my question is really simple. It is possible use Angular Controllers with prototype? 'use strict'; var EventController = function(scope, EventModel) { this.scope = scope; this....

  1. in angular controller
  2. angular use controller from another module

I have a little FTP script which basically transfer an entire directory tree (by walking it with fs.readdir) to an FTP server one file at a time (I have to do some analysis on each file as it's ...

I have a little FTP script which basically transfer an entire directory tree (by walking it with fs.readdir) to an FTP server one file at a time (I have to do some analysis on each file as it's ...

  1. node make synchronous http request
  2. node make synchronous query
  3. make node js synchronous
  4. node make function synchronous
  5. node make promise synchronous
  6. make foreach synchronous node js
  7. make request synchronous node js
  8. make node js code synchronous
  9. node make callback synchronous
  10. make function synchronous node js
  11. make for loop synchronous node js
  12. node make asynchronous call synchronous