'JavaScript: why can't I chain Array.prototype.filter with .push()?

If Array.prototype.filter returns an array, why can't I invoke push() on this return value immediately?

Example:

var arr = ["a", "ab", "c", "ad"];
var arr2 = arr.filter(function(elmnt) { return elmnt.indexOf("a") > -1; });
// result: ["a", "ab", "ad"]

arr2.push("aaa");
// result: ["a", "ab", "ad", "aaa"]

Ok so far.

But what about chaining that push() call to the filter() call?

var arr = ["a", "ab", "c", "ad"];
var arr2 = arr.filter(function(elmnt) { return elmnt.indexOf("a") > -1; }).push("aaa");
// result: 4

Why does chaining filter() and push() result in the number of elements that I would expect, rather than an array of those elements?



Solution 1:[1]

I suggest you use concat();

var arr = ["a", "ab", "c", "ad"], arr2;
(arr2 = arr.filter(function(elmnt) { return elmnt.indexOf("a") > -1; })).concat("aaa");
// now arr2 is ["a", "ab", "ad", "aaa"]

Solution 2:[2]

now run the above code and see the result/error.

Analyze the difference between your answer before and after running the code

Q2. correct the code so that method chain starts working

function filterOddNumbers(num) {
    if (num % 2 === 0) {
        return true;
    } else {
        return false;
    }
}

const evenNumbers = [1, 2, 3, 4, 5].push().filter(filterOddNumbers);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bugs
Solution 2 meewog