How to insert values from an array into select json keys

I have this array:
[test, usera, test, userb, test, userc, test, userd]

And this JSON:

{
"data": [
    {
        "email": "testusera@abc.com",
        "mobile": "9881880455",
        "panCardNo": "ABCD014141",
        "city": "Mumbai"
    },
    {
        "email": "testusera@abc.com",
        "mobile": "9881880455",
        "panCardNo": "ABCD014141",
        "city": "Mumbai"
    }
]
}

I’m trying to add key-value pairs into existing objects i.e “lastname”:”value from array”

for(var i=0; i<importedJson.data.length;i++)
{
    for(var f=1;f<=arrFirstLast.length-1;f+2)
    {
       importedJson.data[i].lastName = arrFirstLast[f];
    }
}

Expected Json:

{
"data": [
    {
        "lastname": "usera",
        "email": "testusera@abc.com",
        "mobile": "9881880455",
        "panCardNo": "ABCD014141",
        "city": "Mumbai"
    },
    {
        "lastname": "userb",
        "email": "testusera@abc.com",
        "mobile": "9881880455",
        "panCardNo": "ABCD014141",
        "city": "Mumbai"
    }
]
}

I’m getting Undefined? or no ouput?


Source: json

Leave a Reply