JSON Challenge with JS

Asked

Viewed 56 times

0

I have a file that JS file, I need to check the numbers that were skipped in sequence, separated by CNPJ, as would be done?

[
{"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"},

1 answer

1


You can organize your array initial with the function reduce transforming into an object with its respective items. After that, traverse your organized object discovering the minimum and maximum. Having this, check the numbers of that range by checking which are not in your array original:

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));


reduce

The method reduce() performs a function reducer (provided by you) for each member of the array, resulting in a single return value.

Example:

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

  • if I want to separate by series the numbers that are missing, series 1: 3.4 , series 2: 5 , series 3: 3.6.7

  • @Marcospaulo there complicates a lot... has to ask another question for this?

  • I can, I’ll do it now

Browser other questions tagged

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