Answer the question
In order to leave comments, you need to log in
How to filter an array by hasOwnProperty?
Good day!
I have an array where there are sub-arrays with a "last_seen" field.
I need to check that if there is no "last_seen" subarray anywhere in the array, then execute some code.
Arrays and subarrays:
For if I check like this:
if($humans.hasOwnProperty('last_seen')) {
return $humans.last_seen.time < (time - $hours)
}
Answer the question
In order to leave comments, you need to log in
Option 1 :
const $humans = await data.response.items;
if($humans.some(entry => entry.hasOwnProperty('last_seen'))) {
// Всё еще массив, так что надо еще найти тот элемент, в котором есть этот ключ
} else {
// Всё еще массив
}
const $humans = await data.response.items;
const $human = $humans.find(entry => entry.hasOwnProperty('last_seen'));
if($human !== undefined) {
// Первый элемент из массива с ключём last_seen
} else {
// Какой-то код
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question