按多个匹配值过滤数组

时间:2022-09-25 07:58:25

I have been searching through * 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

您可以看到有3个时间帧: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.

注意:j等于来自AJAX查询的JSON解析服务器响应,该查询与php脚本对话,后者传回JSON响应,即上面的数组。

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.

目标:我想要了解的方法是一次过滤数组的多个“时间帧”值,这样当用户点击三个时间帧中任意一个的组合的复选框时,我可以从中取出值复选框并按用户选择的复选框的任意组合过滤数组。恩。 “M1”,“M5”,“M15”,“M1,M5”或M1,M15“或M5,M15”或“M1,M5,M15”等。

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)

返回/过滤的新数组将传递给每个循环的jquery,然后将遍历数据并将数据附加到div。 (我已经把这部分工作了)

Appreciate any guidance!

感谢任何指导!

1 个解决方案

#1


1  

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

您可以组合Array.prototype.filter和Array.prototype.some来过滤掉结果 - 请参阅下面的演示:

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

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

#1


1  

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

您可以组合Array.prototype.filter和Array.prototype.some来过滤掉结果 - 请参阅下面的演示:

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

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}