Array javascript - know number of agencies I have

Asked

Viewed 32 times

-2

I needed to know how many agencies I have in my json file and their respective numbers, in case I know they are 4, but the return being given is an array of 120 accounts only, and I would like an array with the 4 agencies, if anyone can help.

const ordenaAgencia=async()=>{
  const data = await getData();
  return await data.sort((item1, item2)=>item1.agencia-item2.agencia)  
};

const maiorCadaAgencia=(dados)=>{
  const agencias = [];
  for(var i=0; i<dados.length; i++){
    if(i == 0){
      if(dados[i] != dados[i+1]){
        agencias.push(dados[i].agencia)
      }
    } else if(dados[i] != dados[i-1]){
     agencias.push(dados[i].agencia)
    }
  }
  console.info(agencias)
}

ordenaAgencia().then(dados=>{
  maiorCadaAgencia(dados)
});
  • Forget about guys, I think I just found the shit I did, I forgot to compare the agencies and I was just comparing the data

  • You can post the answer to your question, so you can help others who have been through the same problem.

1 answer

0

Hello!

It would be interesting to do a search in the agency array to know if that agency has already been launched. I didn’t test it, but you could see if it works:

const maiorCadaAgencia=(dados)=>{
  const agencias = [];
  for(var i=0; i<dados.length; i++){
    if(agencias.findIndex(dados[i].agencia) == -1) // Procura o número da agência
    {
      // Se não for encontrado, retorna -1. Assim você pode lançar essa agência no array.
      agencias.push(dados[i].agencia)
    }
    // Não há implementação de ELSE, pois se o resultado for 0 ou maior, é porque você
    // já lançou aquela agência no array

    
  }
  console.info(agencias);
}

Success! Answer if it worked...

Browser other questions tagged

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