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

91
Visualizações
trying to convert array of list to object?

i am trying to convert array key/value pair to an object using map and formEntries function i am getting an error message:'Object.fromEntries is not a function' any idea about the fix

main.js

  const data = {}
  const channelsMap = new Map(details.channels);
    const channels = Object.fromEntries(channelsMap);
  data = channels;

expected output

data : {
 channels: {
   "EM": true,
   "SMS": false
}
}

data

  { details: {
       "channels": [
          {
            "channelType": "EM",
            "isActive": "true"
          },
          {
            "channelType": "SMS",
            "isActive": "false"
          }]
    }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to reduce the details.channels array into an object where the channelType is the key and the isActive is the value.

const original = {
  details: {
    channels: [
      { channelType: "EM"  , isActive: true  },
      { channelType: "SMS" , isActive: false },
    ]
  }
};

const transformed = {
  data: {
    channels: original.details.channels
      .reduce((acc, { channelType, isActive }) =>
        ({ ...acc, [channelType]: isActive }), {})
  }
};

console.log(transformed);
.as-console-wrapper { top: 0; max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Upgrading your Node version to something above v12.0.0 should do it. This method isn't included in your current Node version. Documentation here.

about 4 years ago · Juan Pablo Isaza Relatório

0

first is not allowed to use const when you want to change the value , second this is the code to expected output :

let data = {channels:details.channels.map(channel => ({[channel.channelType] : JSON.parse(channel.isActive) }))};

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