I have an Array of objects (clients) like this:
"{"client_id":"AAA1","contracts":[{"contract_id":"CON1-AAA1","revisions":[{"date":"2018-07-30","status":"First Sign"}]}]}"
I can filter by client_id with no problem:
var query = clients.filter(x => x.client_id == "AAA1");
However, I'd like to filter by revision date or status, I tested doing the following, but I get the error "Uncaught TypeError: Cannot read property 'status' of undefined"
var query = clients.filter(x => x.contracts.revisions.status == "First Sign");
Is it possible to do it this way or Im delusional? :)
You can achieve that by using a combination of an Array#filter
and two Array#some
:
const clients = [{
"client_id": "AAA1",
"contracts": [{
"contract_id": "CON1-AAA1",
"revisions": [{
"date": "2018-07-30",
"status": "First Sign"
}]
}]
}, {
"client_id": "AAA2",
"contracts": [{
"contract_id": "CON1-AAA2",
"revisions": [{
"date": "2018-08-30",
"status": "Second Sign"
}]
}]
}];
let result = clients.filter(cl => cl.contracts.some(c => c.revisions.some(r => r.status == 'First Sign')));
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With