0
var object = {

}

socket.on('call', function(data){
  console.log(data); // On console: { number: 68, name: 'John' }
  object.push(data);
});

In the console.log I get the object just fine. But the push function doesn't seem to be working.

    object.push(data);
            ^
 
TypeError: object.push is not a function

2 Answers2

0

object here is an Object, so not have the push function.

If you want to use an object use object[key] = value; or object.key = value;


Array.push in others hand exists.

var object = [];

object.push(value);
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
0

You can't use push() function for Objects. Actually there is no function named push() for Objects. You need to use an Array if you want to use push.

Hope this code helps.

  var myArray = ['A','B'];
  myArray.push('C');
  console.log(myArray);
  //["A", "B", "C"]

  var myObject = {foo:"bar"};
  myObject.name = "John";
  console.log(myObject);
  //{foo: "bar", name: "John"}
Disapamok
  • 1,426
  • 2
  • 21
  • 27