cancel
Showing results for 
Search instead for 
Did you mean: 

Filter expression on arrays

manohar
Contributor

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

1 ACCEPTED SOLUTION

Aleksandar_A
Contributor III

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.

View solution in original post

4 REPLIES 4

Aleksandar_A
Contributor III

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.

Hi @AleksandarAngelevski, thank you very much. that helped.

I am actually looking for docnames that doesn’t exist in filename array. so I removed ! from your expression;

$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)

but looks like my data is coming bit differently, the docname are coming with characters, how can I compare with out converting to string to replace these characters?

[
  {
    "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"
    ]
  }
]

Also looks like my destination system accepts only _ so after match, need to replace spaces, #, / to _.

Thanks
Manohar

If you need to replace them for the target system, you can do that before the filtering using regex.

The following expression might not catch all of your scenarios but should be a good starting point:

$docname.map(x => x.replace(/(#|\/|\s){1,2}/g,'_'))

BR,
Aleksandar 🙂

Thank you again for your help @AleksandarAngelevski .