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

151
Visualizações
Mapping keys into objects with Javascript

This is the object I have:

years = {1924:2, 1934: 10, 2010: 4}

I would like to transform this into:

years = [{year: 1924, items: 2}, {year: 1934, items: 10}, {year: 2010, items: 4}]

This is the code I came up with so far (which obviously doesn't work):

var years2 = {};

for (var i = 0; i < years.length; ++i) {
    years2.push({ years: [$key] })
};

I am stuggling to find the right way to access the unnamed keys and map these into their own objects.

(I am still learning JS and I know this is basics, but somehow cannot get my head around it.. )

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

0

You can create key value pairs from the object with Object entries and map them to new array

const years = {1924:2, 1934: 10, 2010: 4}


const ans = Object.entries(years).map(([key,value]) => {
  return {year: key, items: value}
})

console.log(ans);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can get entries, then convert to object with map

const years = {1924:2, 1934: 10, 2010: 4}
const result = Object.entries(years).map(([year, items]) => ({year: year, items: items}))
console.log(result)

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