Array.filter的异步问题

时间:2016-12-21 12:54:31

标签: javascript angular asynchronous typescript array-filter

我将Ionic2 / Angular2typescript一起使用,我在过滤数组时遇到问题。

我有

let localTours = [];
...
let newTours = dbTours.filter(x => localTours.indexOf(x) < 0);
localTours.push(newTours);

在此示例中,我的localTours始终为空,因为在过滤之前调用了.push。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的过滤是错误的并且正在过滤一切因此给您一个空数组,或者您的某些函数是异步的,在这种情况下,您将受益于使用async.js库等类似的东西:

oneArray.filter(something, (err, results) => {
   otherArray.push(results);
});

你的东西功能,看起来像:

function something(item, callback) {
  //your filtering logic needs to call the callback function to move onto the next item
}
相关问题