manohar
3 years agoContributor
Filter expression on arrays
hi there, I have this json
[
{
"filename": [
"Coupa_Supplier_SB_Tax_Cert_PO_4501923290",
"Coupa_Supplier_SB_Tax_Cert_PO_4501888888"
],
"id": 16755,
"docid": [
"FOL18 4 EXT48000000000761",
"FOL18 4 EXT48000000000760",
"FOL18 4 EXT48000000000759"
],
"docname": [
"Coupa_Supplier_SB_Tax_Cert_PO_4501999999",
"Coupa_Supplier_SB_Tax_Cert_PO_4501923290",
"Coupa_Supplier_SB_Tax_Cert_PO_4501888888"
],
"docext": [
"pdf",
"pdf",
"pdf"
]
}
]
I need to filter docext, docname, docid based on if filename exist in docname. so end json shoud be
[
{
"filename": [
"Coupa_Supplier_SB_Tax_Cert_PO_4501923290",
"Coupa_Supplier_SB_Tax_Cert_PO_4501888888"
],
"id": 16755,
"docid": [
"FOL18 4 EXT48000000000760"
],
"docname": [
"Coupa_Supplier_SB_Tax_Cert_PO_4501923290",
],
"docext": [
"pdf"
]
}
]
I understand I need to use filter expression but need some guidance.
Thanks
Manohar
Hi @manohar.
In the example you have listed, If i understand correctly don’t we also need to include the filename: Coupa_Supplier_SB_Tax_Cert_PO_4501888888? Because it exists in the docname array?
If yes, then you can use the following expressions:
$docname.filter((x,ind,arr) => $filename.indexOf(x) != -1) $docid.filter((x,ind) => $filename.indexOf($docname[ind]) != -1) $docext.filter((x,ind) => $filename.indexOf($docname[ind]) != -1)
Let me know if this helps.
BR,
Aleksandar.