I am using javascript, I have the following structure:
var datos =[
"01|FA|4.0|DEMO|162564|99|30001000000400002463||2750.00|0.00|MXN|1|2730.00|Ingreso|PPD|72010||EMISOR|H&E951128469|HERRERIA & ELECTRICOS SA DE CV|601|RECEPTOR|PASF690721EL6|FRANCISCO ALEJANDRO PAREDES SIERRA|||S01||440.00|440.00|INFO_ADIC||PROLONGACION DE LA 27 NORTE||ORQUIDEAS 620 BUGANBILIAS PUEBLA PUEBLA MEXICO C.P 72580||N ",
"01|CFDI40|01|INFO_GLOBAL||||EMISOR||RECEPTOR|72580|626",
"02|01|5CB8D806-7BDF-4D24-AC4C-4C469EB4F57A|6CB8D806-7BDF-4D24-AC4C-4C469EB4F57A",
"02|04|7CB8D806-7BDF-4D24-AC4C-4C469EB4F57A|8CB8D806-7BDF-4D24-AC4C-4C469EB4F57A",
"03|1|1|E48|F|78101800||VIAJE DE PARAFINA DEL 26 DE NOVIEMBRE DE 2021 CON 17,000 KG DE PESO|2750.00|0.00|2750.00||02",
"03-IMP|TRASLADO|2750.00|002|Tasa|0.160000|440.00",
"03-IMP|RETENCION|2750.00|002|Tasa|0.160000|440.00",
"03-ADU|21 80 1871 0999999",
"04|TRASLADO|002|Tasa|0.160000|440.00|2750.00",
"04|RETENCION|002|||440.00",
"05|1.0|20.00|0.00|",
"05|R|Descripcion del impuesto local|0.50|20.00",
];
let datosExcluidos = [];
datos.forEach(index =>{
var separador = index.slice(index,1);
datosExcluidos.push(separador)
});
console.log(datosExcluidos)
What I really want is to separate the first position, which would be something like this:
01 , 01, 02, 02 .....
But I have not managed to do it since I only get this:
["0", "0", "0", "0", "0", "0", "0", ...]
I tried something like that var separador = index.slice(index,1).split('|');
but it keeps doing the same thing, and I really don't know how to separate only the first position before |
.
SOLUTION:
With split look for it to extract everything, while fulfilling the condition that if it finds '|' and it was the first position (position [0] which was the one it was looking for), and it is added to the array!
It uses regular expressions, in this case we start by parsing the string so that it starts with numbers (at least one) and stops when it finds any other character that is not a digit.
Doing it this way will allow you to forget about some exceptions such as variability in the number of digits.
I have modified some values and you can see the regular expression below:
If for some reason you would like the values to be as
3
instead of03
you can cast the value to integer withparseInt(matches)
;It can also be done a little more handmade for example: