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,503
  • 6
  • 39
  • 59
  • 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
  • 34,157
  • 52
  • 174
  • 280
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,189
  • 2
  • 28
  • 43
1

push is a method for arrays, not objects

Sterling Archer
  • 21,348
  • 17
  • 79
  • 113