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

158
Visualizações
Mapping through an array to produce an object

I have an array :

[
"2022-05-20",
"2022- 06-22",
"2022-06-20"
]

and I want to produce an object like this:

{
    '2022-05-20': {disabled:true},
    '2022-06-22': {disabled: true},
'2022-06-20': {disabled: true},
  }

I tried using a for loop but it kept producing errors. Is this possible with javascript?

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

0

You can use Array#reduce as in the following demo. You can also use Array#map but you would have to use Object.fromEntries as well.

const input = [ "2022-05-20", "2022- 06-22", "2022-06-20" ],

      output = input.reduce(
          (prev,cur) => 
          ({...prev,[cur]:{disabled:true}}), {}
      );
      
      
console.log( output );

USING Array#map ...

Here is how you can use Array#map:

const input = [ "2022-05-20", "2022- 06-22", "2022-06-20" ],

      output = Object.fromEntries(
          input.map(date => [date, {disabled:true}])
      );
      
      
console.log( output );

about 4 years ago · Juan Pablo Isaza Relatório

0

This might get the job done.

const yourArray = ["2022-05-20", "2022-06-22", "2022-06-20"];
const obj = {};
for(const x of yourArray) obj[String(x)] = { disabled: true };
console.log(obj); // :)

Create the variable obj that is going to save the produced object you want. Iterating throw your array and using a string parsed version of the value in the current iteration (parsing just in case, if you already know the array is made of strings, this is kinda unnecessary) to save it as a key on the new object, also assigning to that key, the value { disabled: true }.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a one liner solution:

let res = data.reduce((acc, curr) =>(acc[curr] = {disabled: true}, acc), {});
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