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
How to get the key name dynamically from the list in Nodejs

How to get the key name dynamically from the list and push it to an array using NodeJS.

This is my string { PRICE: '12', TYPE: 'Electronics' }

I want to push the key name PRICE, TYPE to an array like below:

const myArray = ["PRICE", "MyOrg::PRICE", "TYPE", "MyOrg::TYPE"]

I don't know how to get the key name dynamically from the list and push it to an array - can someone pls help me with this. Thanks.

Sample code:

const test = async () => {

        const myList =  { TYPE: '12', REGION: 'Electronics' }

        // Execpted array like below
        const myArray = ["PRICE", "MyOrg::PRICE", "TYPE", "MyOrg::TYPE"]


  };
  
  test();
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could simply do Array.concat with Object.keys

const myList = { TYPE: '12', REGION: 'Electronics' };
let myArray = ['PRICE', 'MyOrg::PRICE'];

myArray = myArray.concat(Object.keys(myList));
console.log(myArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

Question doesn't really make sense. The following code generates the output that you are asking for, but it doesn't seem useful.

const myList = { "PRICE": 12, "TYPE": "Electronic" };

const myArray = [];

Object.keys(myList).forEach( function (key) {
    // Loop through all the keys
    myArray.push(key)
    myArray.push("MyOrg::"+key)
})

console.log(myArray)
// Expected output
// myArray == ["PRICE", "MyOrg::PRICE", "TYPE", "MyOrg::TYPE"]

about 4 years ago · Juan Pablo Isaza Relatório

0

You just need variable substitution in your code, that you get by enclosing variable with curly braces like this ${variable}.

const myList =  { TYPE: '12', REGION: 'Electronics' }
array = Object.keys(myList)
myArray = []
for (let element of array){
    myArray.push(element, `MyOrg::${element}`)
}
console.log(myArray)

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