I
I
Ilkhomjon Matazimov2020-04-25 11:45:56
JavaScript
Ilkhomjon Matazimov, 2020-04-25 11:45:56

How to filter js array?

I have an array that needs to be filtered.
The usual Array.prototype.filter() does not work, because there is one more subarray in the array.

Array example:
5ea3f81742845611836369.png

Here is a subarray:
5ea3f82c617ef344705382.png

I need to filter by time, I thought it would be like this:

$humans = await alles.filter(all => all.last_seen.time > (time() - $1day))


Throws an error that time- is undefined. I tried to put down an asynchronous request everywhere, but all to no avail.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-04-25
@mr_qpdb

const date = new Date();
const time = Math.floor(date.getTime() / 1000);
const $1day = 24 * 60 * 60;
const filtered = array.filter(entry => {
  if (entry.hasOwnProperty('last_seen')) {
    return entry.last_seen.time > (time - $1day);
  }
  return false;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question