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

203
Visualizações
Javascript updating property in object results to updating all properties?

Im used to php syntaxing and cant seem to understand why im not able to solve my problem with something so simple... so here it goes. (probably im overlooking something really stupid but at this point its like a bull on a red cloth)

My code :

n_machines = global.get("n_machines");

endtime = [];
running = [];
time_left = [];
types = ["delayed_output_timer","startup_timer"];

timer = {};


for(i = 0 ; i < types.length ; i++){
    
    key = types[i];

    //loop through parameters of timer and set default values
    for(m_num = 0; m_num < n_machines; m_num ++)
    {
        endtime[m_num] = 0;
        running[m_num] = false;
        time_left[m_num] = 0;
    }
    
    timer[key] = { endtime: endtime, running : running, time_left : time_left };
}

timer.delayed_output_timer.running[0] = "TESTING";

test = timer['delayed_output_timer'].running[0];

msg = {payload:[timer,test],topic:"timer"};

return msg;

When I output this code this returns an object with not only the timer.delayed_output_timer.running[0] changed but alos with the object timer.startup_timer.running[0] changed???

What am I doing wrong here? I don't want to change it to an array. I want to understand why and how to change 1 of these objects arrays... not both?

Also note that Im outside the loop when Im changing it an I have the same problem when I try to change timer["delayed_output_timer"].running[0] = "TESTING" Here is the output. (its in node red console but that shouldnt matter)

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

0

You should probably move the variables endtime, running, and time_left to the scope of the loop. Otherwise if you keep them global, each timer uses that single global object declared at the top.

n_machines = global.get("n_machines");

types = ["delayed_output_timer","startup_timer"];

timer = {};


for(i = 0 ; i < types.length ; i++){
    endtime = [];  /// moved here
    running = [];
    time_left = [];

    key = types[i];
...
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