cancel
Showing results for 
Search instead for 
Did you mean: 

Adding element to array

Ajay_Chawda
Contributor

Input json :-[
{
“e_no”: “1”,
“email”: “abc@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “B”,
“data”: [
{
“e_no”: “12”,
“email”: “abc4@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “c”
},
{
“e_no”: “13”,
“email”: “abc3@gmail.com”,
“country”: “US”,
“firstname”: “A”,
“last name”: “d”
},
{
“e_no”: “14”,
“email”: “abc2@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “n”
},
{
“e_no”: “15”,
“email”: “abc1@gmail.com”,
“country”: “UK”,
“firstname”: “A”,
“last name”: “f”
}
]
},
{
“e_no”: “4”,
“email”: “a@gmail.com”,
“country”: “India”,
“firstname”: “c”,
“last name”: “D”
},
{
“e_no”: “5”,
“email”: “b@gmail.com”,
“country”: “US”,
“firstname”: “C”,
“last name”: “D”
}
]

Output JSON required :-
[
{
“e_no”: “1”,
“email”: “abc@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “B”,
“data”: [
{
“e_no”: “12”,
“email”: “abc4@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “c”,
“age”:“12”
},
{
“e_no”: “13”,
“email”: “abc3@gmail.com”,
“country”: “US”,
“firstname”: “A”,
“last name”: “d”,
“age”:“13”
},
{
“e_no”: “14”,
“email”: “abc2@gmail.com”,
“country”: “India”,
“firstname”: “A”,
“last name”: “n”,
“age”:“14”
},
{
“e_no”: “15”,
“email”: “abc1@gmail.com”,
“country”: “UK”,
“firstname”: “A”,
“last name”: “f”,
“age”:“15”
}
]
},
{
“e_no”: “4”,
“email”: “a@gmail.com”,
“country”: “India”,
“firstname”: “c”,
“last name”: “D”
},
{
“e_no”: “5”,
“email”: “b@gmail.com”,
“country”: “US”,
“firstname”: “C”,
“last name”: “D”
}
]

wanted to add element in array with values of e_no.

image

5 REPLIES 5

The extend method let’s you extend the object with more than one field, so you could do

$.data.map(e => e.extend({'age': e.e_no, 'email': e.email, 'country': e.country....}))