我在客户端上运行了这段代码来过滤事件列表:
if (res)
{
    eventList.filter(function(event) {
        const out = res.find(function(visibility) { return visibility.ID == event.id; }) == undefined;
        return out;
    });
    alert(eventList);
}
displayEvents(eventList);
问题是,即使out是false该元素没有被过滤掉.
只是为了调试我试图return false在任何情况下,结果数组仍然有所有的初始元素:
eventList.filter(function(event) {
    return out;
});
我在这做错了什么?
编辑:
res是ID服务器返回的JSON对象数组(仅包含字段),同时eventList是Facebook事件的列表,从Facebook API请求传递给此回调函数
Array.prototype.filter不会更改数组inplace,它返回由满足提供的谓词的项组成的新数组.它看起来应该是这样的
var result = eventList.filter(function(event) {
    return res.find(function(visibility) { return visibility.ID == event.id; }) === undefined;
});
您不需要声明和赋值变量然后从函数返回它,您只需返回表达式即可
| 归档时间: | 
 | 
| 查看次数: | 1620 次 | 
| 最近记录: |