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

242
Visualizações
Javascript variable from for loop does not get called in push method

I tried doing items.map((item) => arrs.push({item._id:item.word})); but the first element for the key did not work at all so I instead wrote a basic for loop. Now creating the for loop works fine and console logging arrs after the loop shows the correct information for the values but the keys for all of them show "temp" as a string, instead of the desired id from items[i]._id. In VS code the temp in let temp=item... is greyed out and says "temp is declared but its value is never read". Now if I put a console.log(temp) between let temp... and arrs.push... it correctly logs out the value in the temp variable.

 const arrs = [];
  for (let i = 0; i < items.length; i++) {
    let temp = items[i]._id;
    arrs.push({ temp: items[i].word });
  }

I thought temp was within the same scope so it should work. Have no idea what is happening.

I tried creating different scopes but nothing worked.

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

0

You're declaring the property name to be "temp", not assigning it as a value. If you're trying to make a property name be the value of temp, you can use a computed property name by surrounding it with square brackets [].

const arrs = [];
for (let i = 0; i < items.length; i++) {
  arrs.push({[items[i]._id]: items[i].word});
}

This will create an object with a key items[i]._id and value items[i].word. Better yet, you can use map:

const arrs = items.map(item => { return {[item._id]: item.word}});
about 4 years ago · Juan Pablo Isaza Relatório

0

Follow this code snippet, created an object and pushing them in arrs in loop:

 let items = [
            {_id:'1',
                word:'test'
            },
            {_id:'2',
                word:'test2'
            },
            {_id:'3',
                word:'test3'
            },
            {_id:'4',
                word:'test4'
            }
            ];

    let arrs = [];        
    for (var i = 0; i < items.length; i++){
      var obj = {
          [items[i]._id] : items[i].word
      }
      arrs.push(obj);
    }



    console.log(arrs)

 let items = [
            {_id:'1',
                word:'test'
            },
            {_id:'2',
                word:'test2'
            },
            {_id:'3',
                word:'test3'
            },
            {_id:'4',
                word:'test4'
            }
            ];

    let arrs = [];        
    for (var i = 0; i < items.length; i++){
      var obj = {
          [items[i]._id] : items[i].word
      }
      arrs.push(obj);
    }

    console.log(arrs)

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