Javascript Array.prototype.filter()不起作用

Sne*_*ppy 1 javascript arrays

我在客户端上运行了这段代码来过滤事件列表:

if (res)
{
    eventList.filter(function(event) {

        const out = res.find(function(visibility) { return visibility.ID == event.id; }) == undefined;
        return out;
    });

    alert(eventList);
}

displayEvents(eventList);
Run Code Online (Sandbox Code Playgroud)

问题是,即使outfalse该元素没有被过滤掉.

只是为了调试我试图return false在任何情况下,结果数组仍然有所有的初始元素:

eventList.filter(function(event) {

    return out;
});
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

编辑:

resID服务器返回的JSON对象数组(仅包含字段),同时eventList是Facebook事件的列表,从Facebook API请求传递给此回调函数

And*_*myk 5

Array.prototype.filter不会更改数组inplace,它返回由满足提供的谓词的项组成的新数组.它看起来应该是这样的

var result = eventList.filter(function(event) {
    return res.find(function(visibility) { return visibility.ID == event.id; }) === undefined;
});
Run Code Online (Sandbox Code Playgroud)

您不需要声明和赋值变量然后从函数返回它,您只需返回表达式即可