I have the array which I want to convert it into required json format
Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [3] => [email protected] [4] => [email protected] )
required json format
[
{id:0,text:"[email protected]"},
{id:1,text:"[email protected]"},
{id:2,text:"[email protected]"},
{id:3,text:"[email protected]"},
{id:4,text:"[email protected]"}
]
I have tried as
var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
jsonObj["id:" + (i+1)] ,"text:" sampleArray[i];
}
console.log(jsonObj);
getting error.How to convert it into required form
const yourNewArray = yourOriginalArray.map((item, index) => { id: index, text: item })jsonObj["id:" + (i+1)] = "text:" + sampleArray[i];