Javascript Array filter not filtering

前端 未结 2 860
有刺的猬
有刺的猬 2021-01-26 05:53

I cannot get Javascript to filter a dynamically created array, although it filters a static array fine. Code block #1 works (static array), code block #2 doesn\'t (dynamically

相关标签:
2条回答
  • 2021-01-26 06:11

    Or, we can actually do

    Array.prototype.filter.call(_eventsArray, function(_calendarEvent) { return _calendarEvent.unit == _unitValue; });

    0 讨论(0)
  • 2021-01-26 06:27

    _eventsArray in your second example is an HTMLCollection not a JS Array. You can convert it like the following:

    function getRowCalendarEvents (_eventsArray,_unitValue) {
        var arr = [].slice.call(_eventsArray);
    
        return arr.filter(function(_calendarEvent) {
        return _calendarEvent.unit == _unitValue;
      }); 
    }
    
    0 讨论(0)
提交回复
热议问题