How to push string array to json object?

0

Issue

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

Solution

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

Answered By – Abdulah Proho

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More