0

I get the following error on data.push. Why?

Uncaught TypeError: undefined is not a function

when doing this in javascript

var data = ({"name": "button", "value": "delete"});
data.push({"id": 456});

console.log(data);

$.ajax({
    type: ...
    url: ...
    data: data,
    dataType: "json"
}).done(function(data) {
    ...
}).fail(function(data) {
    ...
});
Marco
  • 2,687
  • 7
  • 45
  • 61
  • 2
    `data[key]=value` lets you specify a key+value for objects, whereas push() only specifies a value for Arrays. – dandavis Apr 07 '15 at 22:13

3 Answers3

2

Try this instead. Your data is an object, not an array:

var data = {"name": "button", "value": "delete"};
data.id = 456;
console.log(data);

http://jsfiddle.net/orf40c66/

Somewhat related: How to set a Javascript object values dynamically?

Community
  • 1
  • 1
Johan
  • 35,120
  • 54
  • 178
  • 293
1

Actually data is not an array, but an object. Array must be declared inside square brackets.

var data = [{"name": "button", "value": "delete"}];
console.log(data);
DrKey
  • 3,365
  • 2
  • 29
  • 46
1

push is a method for arrays, not objects

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118