How to extract the value from the object of arrays in js?

I need help to extract the codes from a pdf file that has been converted into a json object. Is it possible to create new array with only fs codes like this FS0003918245, FS0003940457, FS0003898043. From the existing one make a new one with fs codes only. thank you

let array = ["FS0003918245", "FS0003940457", "FS0003898043"]

let array = [
  [
    "1.         FS0003918245\nBr. paketa: 1\nPovrat pošiljke - DS\nFS0003918245",
    null,
    "Primalac\nNaziv: Ljubica Čeman\nAdresa: Despota Stefana 2, 8 sprat Mesto: 21000 Novi Sad\nTelefon: 38163488007",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "Otkupnina:\nVrednost: Plaćanje: Cena:",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "10.000,00\n0,00\nNalogodavac Virman 210,00",
    null,
  ],
  [
    "Referenca: 1764801 ; MOZE KURIR DA OSTAVI PAKET U PRODAVNICU GAZDARICI SASKI",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
  ],
  [
    "2.         FS0003940457\nBr. paketa: 1\nPovrat pošiljke - DS\nFS0003940457",
    null,
    "Primalac\nNaziv: Marija STANKOVIC Adresa: Dedobarski put bb Mesto: 16220 Grdelica\nTelefon: 381637775928",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "Otkupnina: Vrednost: Plaćanje: Cena:",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "7.000,00\n0,00\nNalogodavac Virman 210,00",
    null,
  ],
  [
    "Referenca: 1791153 ; Nazvati 1h prije isporuke",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
  ],
  [
    "3.         FS0003898043\nBr. paketa: 1\nPovrat pošiljke - DS\nFS0003898043",
    null,
    "Primalac\nNaziv: Dusan glisis Adresa: Olge Penavin 8 Mesto: 21000 Novi Sad Telefon: 381606369616",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "Otkupnina: Vrednost: Plaćanje: Cena:",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    "8.000,00\n0,00\nNalogodavac Virman 210,00",
    null,
  ]
];

Upvotes: 0

Views: 66

Answers (1)

J.F.
J.F.

Reputation: 15177

You can simply use this code:

const regex = /FS\d{10}/g
const result = array.map(innerArray => {
    return innerArray.filter(f => regex.test(f)).map(m=>{
        return m.match(regex)
    })
}).flat(Infinity)

Or this one what do the same:

const regex = /FS\d{10}/g
const result = array.map(innerArray => {
    return innerArray.map(m => {
        return m?.match(regex)
    })
}).flat(Infinity).filter(Boolean)

Or to avoid duplicates you can also add:

const resultWithoutDuplicates = [...new Set(result)]

Upvotes: 2

Related Questions