user3259138 user3259138 - 2 years ago 183
JSON Question

Array.some is not a function

I am attempting to pass 2 arrays to a function that filters through array 1 by array 2, then returns the filtered results.

I am not understanding why I am getting the error: "Uncaught TypeError: arr2.some is not a function"

Function Code:

function filterTF(arr1,arr2)
return arr1.filter(function(el)
return arr2.some(function(e)
return el.timeframe == e;

Array 1:

{"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"}

Array 2:


Code that Runs the Function:

var timeframearr = localStorage.getItem("timeframe");

if(timeframearr.length === 0)
console.log("Wings - Timeframe Set: False");
var timeframearr = [];
var j = filterTF(j,timeframearr);

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. timeframearr is either created new if its length is equal to 0, or if it exists in local storage then it gets it from there.

Code that puts timeframearr in local storage:

if ($('#timeframe-checkbox1').is(":checked"))
var t1 = $('#timeframe-checkbox1').val();

if ($('#timeframe-checkbox2').is(":checked"))
var t2 = $('#timeframe-checkbox2').val();

if ($('#timeframe-checkbox3').is(":checked"))
var t3 = $('#timeframe-checkbox3').val();

localStorage.setItem("timeframe", timeframearr);

Goal: What I am trying to accomplish/understand is 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!

Answer Source

I think the issue is your localStorage - this will convert your array to a string (comma separated). What you should do is stringify before setting localStorage and parse on the way out:

localStorage.setItem("timeframe", JSON.stringify(timeframearr));

And pull it out as an array

var arr = JSON.parse(localStorage.getItem("timeframe"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download