Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.reject(): TypeError: ....reject is not a function

Tags:

javascript

I am taking a cool online course about functional programming in JavaScript. I was following along just fine until the instructor used the Array.prototype.reject() and it did not work for me at run time.

I would like to use "reject" instead of a for loop because it is less code. But, my browser, NodeJS and Express Code consoles tell me that reject is not a function.

I researched other articles that discuss promise.reject is not a function, but provide solutions that do not make sense to my scenario.

Here is the example code in the course:

var animals = [
    { name: 'Fluffykins',   species: 'rabbit' },
    { name: 'Caro',         species: 'dog' },
    { name: 'Hamilton',     species: 'dog' },
    { name: 'Harold',       species: 'fish' },
    { name: 'Ursula',       species: 'cat' },
    { name: 'Jimmy',        species: 'fish' }
];


var isDog = function(animal){
    return animal.species === 'dog';
}

var otherAnimals = animals.reject(isDog);

The work-around with a for-loop:

var notDogs = animals.filter(function(animal){
    return animal.species !== 'dog';
});

Its output is:

> notDogs
[ { name: 'Fluffykins', species: 'rabbit' },
  { name: 'Harold', species: 'fish' },
  { name: 'Ursula', species: 'cat' },
  { name: 'Jimmy', species: 'fish' } ]

Please help me use Array.prototype.reject().

EditL

I found Array.prototype.reject() at GitHub/Array.prototype.reject)

like image 870
host_255 Avatar asked Nov 13 '16 11:11

host_255


People also ask

What is reduce () in JavaScript?

reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. Syntax: array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

How do you filter an array?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do you reject a Promise?

You can reject a promise with any value, not just an error object. const p = Promise. reject(42); return p.

How filter function works in JavaScript?

The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.


2 Answers

Array.prototype.reject isn't a thing unless a library/custom code adds the reject method to Arrays.

To achieve what you want, you should use the Array.prototype.filter method like you already are. I'm not sure what you think is longer about it because you can write it the same way:

var animals = [
  { name: 'Fluffykins', species: 'rabbit' }, 
  { name: 'Caro', species: 'dog' }, 
  { name: 'Jimmy', species: 'fish' }
];

function noDogs(animal) {
  return animal.species !== 'dog';
}

var otherAnimals = animals.filter(noDogs);
console.log(otherAnimals);
like image 116
JVDL Avatar answered Nov 03 '22 18:11

JVDL


As mentioned above, this was mentioned in a youtube series about functional programming. The video uploader did correct this mistake with an annotation, but you may not have seen that if you were viewing the video on a mobile device.

enter image description here

like image 42
Conor Pender Avatar answered Nov 03 '22 17:11

Conor Pender