How to store various information in JS properly?

Asked

Viewed 138 times

0

I need to store various information in an object or array, the code is thus adding the information by push():

let filaDeEspera = {nome:[], telefone:[], senha:[]}

filaDeEspera.nome.push("João")
filaDeEspera.telefone.push("1122223333")
filaDeEspera.senha.push("001")

//Adicionando outra pessoa
filaDeEspera.nome.push("Maria")
filaDeEspera.telefone.push("4455556666")
filaDeEspera.senha.push("002")

console.log(filaDeEspera)

However, in addition to inefficient (code repetition) I need to keep looking for the information by index of array in order to delete, there is a better way to store this information?

  • 1

    It would not be better to have an array of objects instead of an object with arrays?

  • I don’t really know, this is the question, how I search and delete object information within an array?

3 answers

4

Usually we programmers group the members of the object into something unique and do not keep the members separate. Thus, the queue would be composed of various objects representing people. In the current form your code has an object that has isolated queues with data isolated from people, it seems conceptually wrong.

let filaDeEspera = [];
filaDeEspera.push({nome : "João", telefone : "1122223333", senha : "001"});
filaDeEspera.push({nome : "Maria", telefone : "4455556666", senha : "002"});
console.log(filaDeEspera);

I put in the Github for future reference.

You could even add already in the creation of the type object array, but because it is a queue it seems to me that there will be more additions than an initialized use.

If it is a very complex application it could create some abstraction for it, but it seems that it is not even the case for the little knowledge it has. Could maintain a structure to better control the creation of the object within a pattern.

Maybe you have a hard time with it because it’s not quite there yet to learn these things. There I recommend learning the basics before leaving for more complex things, and preferably in a more structured and accompanied way since it does not yet have knowledge to direct its own studies. An example of error is not using ; at the end of the line. You believe you are right just because there was no error.

  • In Javascript the use of ; at the end of the line is optional. It has always been considered good practice because of the "minifications" that are made in JS files, but in this time of transpilers makes no difference.

  • Actually there are several situations that leave without problem, even not minifying.

  • Maybe I just didn’t find them then. : ) But anyway, I wouldn’t say it’s a mistake not to use them.

2

Can be done with Objects, and create a function to return an object, as in the example below:

const filaDeEspera = []

const addPessoa = ( _nome, _telefone, _senha ) => {
  return {
    nome: _nome,
    telefone: _telefone,
    senha: _senha
  }
}

filaDeEspera.push( addPessoa( 'maria', '4455556666', '002' ) )

I hope I’ve helped in some way Hug

2


It would be better to group everything into a JSON object. To delete you can use the javascript splice method; To make it easier, I’ve split into functions. See the example below.

let filaDeEspera = [];

function addFilaEspera(item){
  filaDeEspera.push(item);
};


function removerFilaEsperaPorSenha(senha){
   let indiceRemover=null;

   //localiza item
   for(i=0; i<filaDeEspera.length ;i++){
      let item =  filaDeEspera[i];

      if(item.senha == senha){
        indiceRemover= i;
        break;
      }
   }
   //remove item 
   if(indiceRemover != null){
        filaDeEspera.splice(indiceRemover,1);
   }
   
}


addFilaEspera({
   nome: "João",
   telefone: "1122223333",
   senha: "001"
});

addFilaEspera({
   nome: "Maria",
   telefone: "4455556666",
   senha: "002"
});


console.log(filaDeEspera);
removerFilaEsperaPorSenha("001");
console.log(filaDeEspera);

  • You can use the method Array.findIndex() to locate the item: let indiceRemover = filaDeEspera.findIndex(obj => obj.senha === senha)

Browser other questions tagged

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