Kryman Kryman - 3 months ago 23
Javascript Question

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.

Answer

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