user3259138 user3259138 - 2 months ago 15
Javascript Question

Filter Array by Multiple Matching Values

I have been searching through stackoverflow questions to try and understand how to filter an array by multiple matching values.

My Array:

[{"time":"2016-11-30 02:45:00","signal":"Buy","symbol":"AUDNZDecn","price":"1.04605","timeframe":"M15","epoch":"1480473900","candel":"M151480481100"},{"time":"2016-11-30 02:41:00","signal":"Sell","symbol":"AUDJPYecn","price":"84.08700","timeframe":"M1","epoch":"1480473660","candel":"M11480480860"},{"time":"2016-11-30 02:24:59","signal":"Buy","symbol":"EURNZDecn","price":"1.48820","timeframe":"M5","epoch":"1480472699","candel":"M51480479900"}]

You can see there are 3 timeframes: M1, M5, M15

I am able to use the following code to filter by a single "timeframe":

var j = j.filter(function(el) {
return el.timeframe == timeframe;

Note: j is equal to a JSON parsed server response from an AJAX query that talks to a php script, which passes back a JSON response, aka the array above.

Goal: What I want to understand how to do is filter by multiple "timeframe" values of the array at a time, so that when a user clicks a checkbox for a combination of any of the three timeframes, I can pull the value from the checkboxes and filter the array by whatever combination of checkboxes the user had selected. ex. "M1", "M5", "M15", "M1,M5" or M1,M15" or M5,M15" or "M1,M5,M15" and so on.

The new array that is returned/filtered will get passed to a jquery each loop, that will then go through the data and append the data to a div. (I have this part working already)

Appreciate any guidance!


You can combine Array.prototype.filter and Array.prototype.some to filter out the results - see demo below:

var array = [{"time":"2016-11-30 02:45:00","signal":"Buy","symbol":"AUDNZDecn","price":"1.04605","timeframe":"M15","epoch":"1480473900","candel":"M151480481100"},{"time":"2016-11-30 02:41:00","signal":"Sell","symbol":"AUDJPYecn","price":"84.08700","timeframe":"M1","epoch":"1480473660","candel":"M11480480860"},{"time":"2016-11-30 02:24:59","signal":"Buy","symbol":"EURNZDecn","price":"1.48820","timeframe":"M5","epoch":"1480472699","candel":"M51480479900"}];

function filterResults(arr) {
  return array.filter(function(el) {
    return arr.some(function(e) {
      return el.timeframe == e;

// pass the required timeframes to filter here
var result = filterResults(['M1','M5']);