JSON Challenge and JS Separate Items

Asked

Viewed 87 times

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

1 answer

0


If you just need to print the values... this should solve

const imprime = (valores) => {
  for (let [key, value] of Object.entries(valores)) {
    console.log('CNPJ:', key);
    let serie1 = [], serie2 = [];
    for (let [k, v] of Object.entries(value)) {
      serie1.push(k);
      serie2.push(v);
    }
    console.log('Serie 1:', serie1.join(', '));
    console.log('serie 2:', serie2.join(', '));
  }
} 
imprime(organizados);

the complete example you find here

  • and if in the case had one more series, the series 3?

  • probably its structure would be different.... ai would have to change the algorithm, according to its structure is that ai

  • 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

  • In case she would only have the same series 3, would be 3 series

  • How would it be done, if you prefer I can create another question, because I’m cracking my head here kkkkk

  • In reply to your other question https://answall.com/a/370362/80265, the complete code can be found here https://jsfiddle.net/x92was8q/5/

  • There he is not checking when jumped, I will integrate the two codes

  • Check there, I believe that this solution solves both cases.

  • yes, if you can put the two solutions together it would be perfect

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.