I am trying to push
data.push({\"country\": \"IN\"});
as new id and value to a json string. but it gives the following error
make sure you push into an Array only and if their is error like Uncaught TypeError: data.push is not a function** then check for type of data you can do this by consol.log(data) hope this will help
you can use push method only if the object is an array:
var data = new Array();
data.push({"country": "IN"}).
OR
data['country'] = "IN"
if it's just an object you can use
data.country = "IN";
Try This Code $scope.DSRListGrid.data = data; this one for source data
for (var prop in data[0]) {
if (data[0].hasOwnProperty(prop)) {
$scope.ListColumns.push(
{
"name": prop,
"field": prop,
"width": 150,
"headerCellClass": 'font-12'
}
);
}
}
console.log($scope.ListColumns);
Your data
variable contains an object, not an array, and objects do not have the push
function as the error states. To do what you need you can do this:
data.country = 'IN';
Or
data['country'] = 'IN';
I think you set it as
var data = [];
but after some time you made it like:
data = 'some thing which is not an array';
then
data.push('')
will not work as it is not an array anymore.
Also make sure that the name of the variable is not some kind of a language keyword. For instance, the following produces the same type of error:
var history = [];
history.push("what a mess");
replacing it for:
var history123 = [];
history123.push("pray for a better language");
works as expected.