0
I have this code, which checks when a number has been skipped
const dados = [
{ "cnpj": "1234567891011", "serie": "1", "numero": "1" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "2" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "3" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "4" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "5" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "6" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "7" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "9" },
{ "cnpj": "1234567891011", "serie": "1", "numero": "10" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "1" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "2" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "4" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "5" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "6" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "7" },
{ "cnpj": "1213141516171", "serie": "1", "numero": "9" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "10" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "11" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "12" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "15" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "16" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "17" },
{ "cnpj": "1213141516171", "serie": "2", "numero": "21" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "22" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "1" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "2" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "3" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "4" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "6" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "7" },
{ "cnpj": "9090909871234", "serie": "1", "numero": "8" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "2" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "1" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "3" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "4" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "5" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "6" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "7" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "8" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "9" },
{ "cnpj": "9090909871234", "serie": "3", "numero": "10" }
];
const organizados = dados.reduce((acumulador, { cnpj, numero }) => {
const copia = { ...acumulador };
copia[cnpj] = [...(copia[cnpj] || []), parseInt(numero, 10)];
return copia;
}, {});
const faltando = Object.keys(organizados).reduce((acumulador, cnpj) => {
const numeros = organizados[cnpj];
const [minimo, maximo] = [Math.min(...numeros), Math.max(...numeros)];
const copia = { ...acumulador };
copia[cnpj] = Array.from(Array(maximo-minimo), (v, indice) => indice + minimo).filter(item => !numeros.includes(item));
return copia;
}, {});
console.log('CNPJs organizados: ', JSON.stringify(organizados));
console.log('Números faltando: ', JSON.stringify(faltando));
I would like the exit to be organised, for example:
CNPJ: 1234567891011
Series 1: 19
Series 2: 13, 18, 19, 20
and if in the case had one more series, the series 3?
– Marcos Paulo
probably its structure would be different.... ai would have to change the algorithm, according to its structure is that ai
– João Cota
That structure of key: value that you are using it is treated that way ai... If you had series 1, series 2, ... series n, you’d probably have to think of a recursive structure with children, or even a list of lists, to be completely dynamic
– João Cota
In case she would only have the same series 3, would be 3 series
– Marcos Paulo
How would it be done, if you prefer I can create another question, because I’m cracking my head here kkkkk
– Marcos Paulo
In reply to your other question https://answall.com/a/370362/80265, the complete code can be found here https://jsfiddle.net/x92was8q/5/
– João Cota
There he is not checking when jumped, I will integrate the two codes
– Marcos Paulo
Check there, I believe that this solution solves both cases.
– João Cota
yes, if you can put the two solutions together it would be perfect
– Marcos Paulo