Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda