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,
]
];
CodePudding user response:
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)]
CodePudding user response:
I will give you a rodumentary solution, feel free to improve it I have not touch JS in years
......
//most atomic function, extracts the codes from string slices
function ExtractFSContentsFomString(stringElement) {
results = []
pivotStart = 0
checking = false
console.log("FS CONTENTS ON: " stringElement)
for (idx = 0; idx < stringElement.length; idx ) {
if (stringElement[idx] === "F" && stringElement[idx 1] === "S" && !checking) {
checking = true
pivotStart = idx
//increase to avoid S
idx
console.log("FS COUNTING")
} else {
if ((checking && !stringElement[idx].match(/^\d $/)) || (checking && idx == stringElement.length - 1)) {
console.log("closurer: " stringElement[idx])
pivotEndings = idx
checking = false
subSet = stringElement.slice(pivotStart, idx)
results = results.concat(subSet)
}
}
}
console.log('sub results: ' results)
return results
}
//upper level function, gets the main slices chooped and review to obtain final slice
function PDFFSExtractor(mainArray) {
finalArray = []
for (i = 0; i < mainArray.length; i ) {
console.log("interating")
for (subI = 0; subI < mainArray[i].length; subI ) {
//checks string that may containt FS DATA
if (String(mainArray[i][subI])) {
var FSSubARRAYSExtracted = [];
//eg '00 2324 FS024242'
FSSubARRAYSExtracted = ExtractFSContentsFomString(mainArray[i][subI])
if (FSSubARRAYSExtracted.length != 0) {
finalArray = finalArray.concat(FSSubARRAYSExtracted)
}
}
}
}
return finalArray
}
//call out...
function main() {
console.log('hello')
console.log('final results: ' PDFFSExtractor([[""], [0, 0, 0, 2, 3, ' 3 FS000009 vF000009 FS000009', 034, 'F000009'], [], [2, 3333]]))
}
main()
LINK ON PLAYGROUND: https://codepen.io/tor/pen/QWGxRzE