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

95
Visualizações
How to push string array to json object?

I am new to typescript. please help to push the data so here it goes Here the story goes I have string array and i need to push it to the json object

interface LocalIds {
    value: string;
    label: string;
  }

 const localIds = [
    { value: 'test', label: 'test' },
    { value: 'test2', label: 'test2' },
  ];


////////////// HERE in string array that data is coming ///////////
    const localIdentifiers: string[] = result.data.map((item: string) => item);


///////////// I want to push the string array data to json object with label & value////// 
// I experimented alot but giving some type error and all I am not getting 
    localIds.push({ label: 'abc', value: 'abc' });
 localIdentifiers.map(i => localIds.push(...localIds:{value:i,label:i}[])); //ERROR

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

0

Half of your code does nothing useful

  • result.data.map((item: string) => item) will do nothing
  • using map when not returning anything inside it is pointless. At very least use forEach instead. or even better....

You should use map with concat:

interface LocalIds {
    value: string;
    label: string;
}

const localIds = [
    { value: 'test', label: 'test' },
    { value: 'test2', label: 'test2' },
];

localIds.push({ label: 'abc', value: 'abc' });
const finalLocalIds = localIds.concat( result.data.map((i: string) => ({value:i,label:i})) );

Live example

about 4 years ago · Juan Pablo Isaza Relatório

0

Try fixing last line as following

replace ; with , and remove [] at the end

localIdentifiers.map(i => localIds.push(...localIds, {value:i,label:i}));

also, you dont need ...localIds, since it will duplicate current array every time element is pushed to array

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