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

100
Visualizações
How do I create an object nested in a new array using values from an array?

I currently have an array of string values: const array1 = ['string1', 'string2'];

I want to be able to use the strings from this array and create a new array with nested objects that use them as the values for the object keys. Like this:

const array2 = [{value: 'string1', label: 'string1'}, {value: 'string2', label: 'string2' ];

I've tried using the .reduce() method and currently, my code looks like this:

const arr = ['string1', 'string2'];
const newArr = [];

function reducer(acc, cur) {
  return {...acc, [cur.id]: cur};
}

const reducedArr = arr.reduce(reducer, {});
newArray.push(reducedArr);

When I log newArray to the console I'm getting back:

Array [{ undefined: 'string2'}]

What is the best way to solve this problem?

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

0

Each element in array2 is a mapping of each element of array1.

const array1 = ['string1', 'string2'];
const array2 = array1.map(item => ({value: item, label: item}));
console.log(array2);

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