最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.some is not a function - Stack Overflow

programmeradmin1浏览0评论

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:

5,15

Code that Runs the Function:

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

if(timeframearr.length === 0) 
{
    console.log("Wings - Timeframe Set: False");
    var timeframearr = [];
} 
else 
{
    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();
    console.log($('#timeframe-checkbox1').val());
    timeframearr.push(t1);
}

if ($('#timeframe-checkbox2').is(":checked")) 
{
    var t2 = $('#timeframe-checkbox2').val();
    console.log($('#timeframe-checkbox2').val());
    timeframearr.push(t2);
}

if ($('#timeframe-checkbox3').is(":checked")) 
{
    var t3 =  $('#timeframe-checkbox3').val();
    console.log($('#timeframe-checkbox3').val());
    timeframearr.push(t3);
}

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!

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:

5,15

Code that Runs the Function:

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

if(timeframearr.length === 0) 
{
    console.log("Wings - Timeframe Set: False");
    var timeframearr = [];
} 
else 
{
    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();
    console.log($('#timeframe-checkbox1').val());
    timeframearr.push(t1);
}

if ($('#timeframe-checkbox2').is(":checked")) 
{
    var t2 = $('#timeframe-checkbox2').val();
    console.log($('#timeframe-checkbox2').val());
    timeframearr.push(t2);
}

if ($('#timeframe-checkbox3').is(":checked")) 
{
    var t3 =  $('#timeframe-checkbox3').val();
    console.log($('#timeframe-checkbox3').val());
    timeframearr.push(t3);
}

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!

Share Improve this question edited Nov 30, 2016 at 20:06 Daniel Corzo 1,0752 gold badges19 silver badges32 bronze badges asked Nov 30, 2016 at 19:17 user3259138user3259138 4412 gold badges7 silver badges20 bronze badges 8
  • 5,15 - did you mean [5,15] ? - Else, 5,15 is not an array... It's just a string. – tymeJV Commented Nov 30, 2016 at 19:19
  • Are you sure the JSON was parsed? Signs indicate that it wasn't. – Felix Kling Commented Nov 30, 2016 at 19:20
  • tymeJV is right, if you get the value from localStorage you will have to split the string. .split(',') – inoabrian Commented Nov 30, 2016 at 19:21
  • I thought it was. But I will double check. Never hurts to double check. – user3259138 Commented Nov 30, 2016 at 19:21
  • nvm, tymeJV is right. arr2 / local storage is the issue. – Felix Kling Commented Nov 30, 2016 at 19:22
 |  Show 3 more comments

2 Answers 2

Reset to default 6

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

We had this pop up today and in our case we had someone passing an Input variable as a boolean where it was expected to be an array type. So a simple check was put in place before the .some line.

if (Array.isArray(myVar)) {
    // then do stuff
}
发布评论

评论列表(0)

  1. 暂无评论