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

166
Visualizações
How can I create an object based on the keys of an array?

I have this array:

const options = [
    {
        uuid: '123312',
        label: 'hello'
    },
    {
        uuid: '523312',
        label: 'there'
    }
];

Which I need to turn into this: { result: { [uuid-label]: number } }

result: {
 '123312-hello': 10 // this is just a random number for now
 '523312-there': 20
}

The code that I have so far is this:

  const randomIntFromInterval = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min);

  const [result, setResult] = useState<Result>({} as Result);

  useEffect(() => {
    if(options.length) {
      setResult(
        options.map(o => {
          return { [`${o.uuid}-${o.label}`]: randomIntFromInterval(0, 500) }
      }))
    }
  }, [options]);

But that code above is creating an array, like [{'123312-hello': 10}, {'523312-there': 20}]

Check the code snippet:

const options = [{
    uuid: '123312',
    label: 'hello',
    sortOrder: 0
  },
  {
    uuid: '523312',
    label: 'there',
    sortOrder: 1
  }
];

const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

const check = options.map(o => {
  return {
    [`${o.uuid}-${o.label}`]: randomIntFromInterval(0, 500)
  }
});

console.log(check);

So what am I missing?

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

0

Seems a good candidate for Object.fromEntries:

const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
const options = [{uuid: '123312',label: 'hello'},{uuid: '523312', label: 'there'}];

const result = Object.fromEntries(options.map(({uuid, label}) =>
    [`${uuid}-${label}`, randomIntFromInterval(0, 500)]
));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use reduce instead of map:

const options = [{
    uuid: '123312',
    label: 'hello',
    sortOrder: 0
  },
  {
    uuid: '523312',
    label: 'there',
    sortOrder: 1
  }
];

const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

const check = options.reduce((acc, o) => {
  acc[`${o.uuid}-${o.label}`] = randomIntFromInterval(0, 500);
  return acc;
}, {});

console.log(check);

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