简体   繁体   中英

Javascript Array filter not filtering

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 created array):

// global variables

var globalCalendarEventsArray = [
  {element: "box1", unit: "a"},
  {element: "box2", unit: "a"},
  {element: "box3", unit: "b"},
  {element: "box4", unit: "b"}
];

// var globalCalendarEventsArray = document.getElementsByClassName("redBox");

var rowCalendarEventsArray = [];


// functions
function getRowCalendarEvents (_eventsArray,_unitValue) {
   return _eventsArray.filter(function(_calendarEvent) {
   return _calendarEvent.unit == _unitValue;
  }); 
}

function onMouseDblClick () {
    rowCalendarEventsArray = getRowCalendarEvents(globalCalendarEventsArray,"a");
    alert(globalCalendarEventsArray.length);
    alert(globalCalendarEventsArray[0].unit);
    alert (rowCalendarEventsArray.length);
}


// event listeners
window.addEventListener("dblclick",onMouseDblClick,false);


// execution code
var box1 = document.createElement("div");
box1.className = "redBox";
box1.unit = "a";
document.body.appendChild(box1);

var box2 = document.createElement("div");
box2.className = "redBox";
box2.unit = "a";
document.body.appendChild(box2);

var box3 = document.createElement("div");
box3.className = "redBox";
box3.unit = "b";
document.body.appendChild(box3);

var box4 = document.createElement("div");
box4.className = "redBox";
box4.unit = "b";
document.body.appendChild(box4);

However, as soon as I build the array dynamically by using .getElementsByClassName, the filtering falls apart, despite the alert functions confirming that .getElementsByClassName built an array and that when I call the .unit property it is there. Ex:

// global variables
/*
var globalCalendarEventsArray = [
  {element: "box1", unit: "a"},
  {element: "box2", unit: "a"},
  {element: "box3", unit: "b"},
  {element: "box4", unit: "b"}
];*/

var globalCalendarEventsArray = document.getElementsByClassName("redBox");

var rowCalendarEventsArray = [];


// functions
function getRowCalendarEvents (_eventsArray,_unitValue) {
   return _eventsArray.filter(function(_calendarEvent) {
   return _calendarEvent.unit == _unitValue;
  }); 
}

function onMouseDblClick () {
    rowCalendarEventsArray = getRowCalendarEvents(globalCalendarEventsArray,"a");
    alert(globalCalendarEventsArray.length);
    alert(globalCalendarEventsArray[0].unit);
    alert (rowCalendarEventsArray.length);
}


// event listeners
window.addEventListener("dblclick",onMouseDblClick,false);


// execution code
var box1 = document.createElement("div");
box1.className = "redBox";
box1.unit = "a";
document.body.appendChild(box1);

var box2 = document.createElement("div");
box2.className = "redBox";
box2.unit = "a";
document.body.appendChild(box2);

var box3 = document.createElement("div");
box3.className = "redBox";
box3.unit = "b";
document.body.appendChild(box3);

var box4 = document.createElement("div");
box4.className = "redBox";
box4.unit = "b";
document.body.appendChild(box4);

Fiddle: https://jsfiddle.net/kryman/uexo1hs4/

Any help would be appreciated, thank you.

_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;
  }); 
}

或者,我们实际上可以做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM