Async problems whith Array.filter

415 views Asked by At

I'm using Ionic2 / Angular2 with typescript and I have a problem while filtering an Array.

I have

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

In this example my localTours is always empty, because the .push is called before the filtering. Any idea how to solve this?

1

There are 1 answers

0
Deleteman On

Either your filtering is wrong and is filtering EVERYTHING thus giving you an empty array as a result, OR your something function is async, in which case you would benefit from using something like the async.js library and so something like:

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

And your something function, looks like:

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