Boris Callens
Boris Callens

Reputation: 93417

Why does this array.filter always return nothing?

Why does this filter never return any objects?

NewHashMap.prototype.remove = function (keys, obj) {
    // snip
    var myEntries = this.entries;
    var filteredEntries = myEntries.filter(
        function(entry){
            //me.isContainedBy(entry, keys) &&
            //entry.obj === obj
            true;
        });
    console.debug("entries ",  myEntries.length);
    console.debug("filtered ", filteredEntries.length);
    // snip
}

A bit further I use it in a different context and it works.
You also see that I commented out my actual filter requirements and replaced them by a plain True. Same thing.

I'm guessing it's a context issue, but I don't see where.

Upvotes: 2

Views: 328

Answers (2)

João Silva
João Silva

Reputation: 91369

You need to return a boolean value from the callback function:

var filteredEntries = myEntries.filter(
        function(entry){
            //me.isContainedBy(entry, keys) &&
            //entry.obj === obj
            return true;
        });

Upvotes: 3

Michael Berkowski
Michael Berkowski

Reputation: 270775

Your true does nothing unless you return it from the anonymous function:

var filteredEntries = myEntries.filter(
    function(entry){
        //me.isContainedBy(entry, keys) &&
        //entry.obj === obj
        return true;
});

Or with your actual filter code:

var filteredEntries = myEntries.filter(
    function(entry){
        return me.isContainedBy(entry, keys) && entry.obj === obj
});

Upvotes: 3

Related Questions