Read JSON with JS

Asked

Viewed 83 times

-3

I have the following code

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": "1213141516171", "serie": "3", "numero": "2" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "3" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "5" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "6" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "7" },
  { "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;
}, {});

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


console.log('pulou numeros');
imprime(faltando);

which checks where skipped the number separated by CNPJ and serial, the problem is that it is not pulling the number 3 series

2 answers

0

You would like to organize the series according to the "serie" attribute of your object array?

I believe it can be solved as follows:

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": "1213141516171", "serie": "3", "numero": "2" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "3" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "5" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "6" },
  { "cnpj": "1213141516171", "serie": "3", "numero": "7" },
  { "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 distinctCnpj = [...new Set(dados.map(d => d.cnpj))];
const distinctSerie = [...new Set(dados.map(d => d.serie))];

const filterDados = {};

for(let c of distinctCnpj){
	filterDados[c] = []
  for(let s of distinctSerie){
		filterDados[c][s] = dados.filter(d => d.cnpj === c && d.serie === s).map(x => x.numero);
  }
}

const imprime = (valores) => {                      
  for (let [key, value] of Object.entries(valores)) {
    console.log('Cliente:', key);
    for (let [k, v] of Object.entries(value)) {
    	console.log(`Serie ${k}: `, v.join(', '));
    }
  }
}

imprime(filterDados);

  • Your code doesn’t work dados is not defined

  • didn’t work because it doesn’t have the array

  • It follows the correction, in which case it was only to take the array of the question.

  • It’s perfect, but it’s not showing the numbers that jumped, it’s just listing the numbers

  • Just add the condition to display.

0


Adjusting the answer to the question JSON Challenge with JS to work with the series in the key terms the following:

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, serie, numero }) => {
  const copia = { ...acumulador };
  const chave = `${cnpj}-${serie}`;
  copia[chave] = [...(copia[chave] || []), parseInt(numero, 10)];
  return copia;
}, {});

const faltando = Object.keys(organizados).reduce((acumulador, chave) => {
  const numeros = organizados[chave];
  const [minimo, maximo] = [Math.min(...numeros), Math.max(...numeros)];
  const copia = { ...acumulador };
  copia[chave] = 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));

  • perfect, just missed the formatting, but I fix here

  • How would you give a line break?

  • CNPJ: 1234567891011 Series 1: 19 Series 2: 13, 18, 19, 20

Browser other questions tagged

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