Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
If key value pair exists in Object then update it else add it in an object

I have a blank object which I need to update on user action.

let userAction = {};

I have multiple buttons on page with unique ID's,

Like: Btn1, Btn2, whatever

So if as a user I will click Btn1 then my object should be updated with its id and number of click., Like this

userAction = {btn1: 1}

if I will click on bt2 then it would also update that object with its ID like this:

userAction = {btn1: 1, Btn2: 1 }

but if I will click any other button one more time then it should update the value by +1. Like this

So I click Btn1 again.. then object will look like this.

userAction = {btn1: 2, Btn2: 1 }

I tried to do it like this, just need to know is there any better or optimized option to do this.

let userAction  = {};

$('button').click(function(){
  let btnId = $(this).attr('id');
  let exisitngValue = userAction[`${btnId}`];
if (btnId in userAction){
  userAction[`${btnId}`] = exisitngValue+1;
}else {
userAction[`${btnId}`] = 1;
}
console.log(userAction);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="Btn1">Button 1</button>  
<button id="Btn2">Button 2</button>  
<button id="Btn3">Button 3</button>  
<button id="whatever">whatever</button>

Is there any better way to do it?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could take just the id of the button directly without a template string and assign directly the value or zero and add one.

let userAction  = {};

$('button').click(function(){
  let btnId = $(this).attr('id');
  userAction[btnId] = (userAction[btnId] || 0) + 1;
  console.log(userAction);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="Btn1">Button 1</button>  
<button id="Btn2">Button 2</button>  
<button id="Btn3">Button 3</button>  
<button id="whatever">whatever</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

let userAction  = {};

$('button').click(function(){
  let btnId = $(this).attr('id');
  userAction[btnId]=userAction[btnId] ?  userAction[btnId]+1 : 1;
console.log(userAction);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="Btn1">Button 1</button>  
<button id="Btn2">Button 2</button>  
<button id="Btn3">Button 3</button>  
<button id="whatever">whatever</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

let userAction  = {};

$('button').click(function(){
   let btnId = $(this).attr('id');
   userAction[btnId] = userAction[btnId]+1 || 1;

   console.log(userAction);
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="Btn1">Button 1</button>  
<button id="Btn2">Button 2</button>  
<button id="Btn3">Button 3</button>  
<button id="whatever">whatever</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda