Imagine trying to create the array structure shown below programmatically using the push() function in JavaScript:
var arr = [
{id: 1, txt: "First Element"},
{id: 2, txt: "Second Element"},
{id: 3, txt: "Third Element"}
];
My initial attempt was as follows:
var arr = [];
var id = 1;
var text = "First Element";
for (var i=0;i<3;i++){
arr.push({id,text});
}
This approach is incorrect because I neglected to specify the column names. What alternative method should I consider?
Appreciate your insights.