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

142
Visualizações
Rename Key in JS Object with values from another array

I want to rename the keys in the object with the names of the array

let title = [
    'Total Cryptocurrencies',
    'Total Markets',
    'Total Exchanges',
    'Total Market Cap',
    'Total 24h Volume', 
];

var obj = {
    1: 5,
    2: 7,
    3: 0,
    4: 0,
    5: 0,
};

So in the end I want to have my object like this:

var obj = {
    'Total Cryptocurrencies': 5,
    'Total Markets': 7,
    'Total Exchanges': 0,
    'Total Market Cap': 0,
    'Total 24h Volume': 0,
};

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

0

You could do it with a for loop as follows.

let title = [
    'Total Cryptocurrencies',
    'Total Markets',
    'Total Exchanges',
    'Total Market Cap',
    'Total 24h Volume', 
];

var obj = {
    1: 5,
    2: 7,
    3: 0,
    4: 0,
    5: 0,
};

var newObj = {};

for(i = 0; i < title.length; i++) {
    newObj[title[i]] = obj[(i+1).toString()];
}

var obj = newObj;
about 4 years ago · Juan Pablo Isaza Relatório

0

Not sure if it the best solution but you can try

obj= title.reduce((prev, curr, idx) => {return {...prev, [curr]: obj[idx+1]} }, {});

Assuming obj's keys will be numbers will map perfectly with the title indexes

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