I 'm trying to add the data in to json objects using for loop, but push
method is not working because it adds the object in to the json array, Any idea to add datas in to json object?
My Code:
var data = [
{ Name: "Jake", Gender: "Male", Address: "Address1" },
{ Name: "Laura", Gender: "Female", Address: "Address2" },
];
for (var i = 0; i < data.length; i++) {
if (data[i].Gender == "Male") {
data[i].push({ Icon: "image/male.png" });
} else {
data[i].push({ Icon: "image/female.png" });
}
}
var result = data;
I need the result as:
result = [
{ Name: "Jake", Gender: "Male", Address: "Address1", Icon: "image/male.png" },
{ Name: "Laura", Gender: "Female", Address: "Address2", Icon: "image/female.png" },
];
The data Icon
is to be added in the every object based on the if condition