小编Dym*_*mos的帖子

Array.prototype.reject():TypeError:.... reject不是函数

我正在参加一个关于JavaScript中函数式编程的很酷的在线课程.我一直很顺利,直到指导员使用它Array.prototype.reject()并且在运行时它对我不起作用.

我想使用"reject"而不是for循环,因为它代码较少.但是,我的浏览器,NodeJS和Express Code控制台告诉我reject is not a function.

我研究过其他讨论的文章promise.reject不是函数,而是提供对我的场景没有意义的解决方案.

以下是课程中的示例代码:

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);
Run Code Online (Sandbox Code Playgroud)

使用for循环解决方法:

var notDogs = animals.filter(function(animal){
    return animal.species !== 'dog';
});
Run Code Online (Sandbox Code Playgroud)

它的输出是: …

javascript

7
推荐指数
3
解决办法
1万
查看次数

标签 统计

javascript ×1