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

102
Visualizações
How can I create an object mapping from an array of strings?

i am stumbling onto something that should be easy, yet i can't seem to be able to do it.

i have an array of string, and i want to map over it to create an object like so :

const monthesList = ['january', 'february', 'march', 'april', 'may']

const myState = monthesList.map(month => (
    month: {selected: false, year: 2022}
    )
  )

what i want is to get the following object :

myState ={
  january:{ 
    selected: false,
    year: 2022
  },
  february:{ 
    selected: false,
    year: 2022
  },
  march:{ 
    selected: false,
    year: 2022
  },
  april:{ 
    selected: false,
    year: 2022
  },
  may:{ 
    selected: false,
    year: 2022
  },
}

Edit: i just found the way :

const myState = monthesList.map(month => (
    {[month] : {
      selected: false,
      year: 2022
    }}
    )
  )

just need to make an object out of it, shouldn't be too hard

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

0

The .map() method returns an array, but the output that you're after is an object. You need something that can convert an array of key-value pairs (ie: [key, value]) to an object. For this, we have the Object.fromEntries() method that takes an array of key-value pairs, and from it, constructs an object like so:

const monthesList = ['january', 'february', 'march', 'april', 'may'];

const myState = Object.fromEntries(monthesList.map(month => [
  month, {selected: false, year: 2022}
]));
console.log(myState);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use map to generate an array of arrays with the values in each internal array being the month name and object; then use Object.fromEntries to convert that into your desired object:

const monthesList = ['january', 'february', 'march', 'april', 'may']

const myState = Object.fromEntries(
  monthesList.map(m => [m, {selected: false, year: 2022}])
)

console.log(myState)

about 4 years ago · Juan Pablo Isaza Relatório

0

map will only return an array. Your expected output is an object. There's no need to over-complicate things - use a simple loop instead.

const list = ['january', 'february', 'march', 'april', 'may'];

const myState = {};

for (const month of list) {
  myState[month] = { selected: false, year: 2022 };
}

console.log(myState);

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