0

I already have object(called callbackData) which already has [Object,Object,Object,Object] structure.(these are real Object). When I push in callbackData1(which is also object) into callbackData and then look into callbackData, I see that callbackData1 object was pushed in as Array(while previous object remain object).

What am I missing here? Am I pushing this in the wrong way?

console.log("callbackData type is  " + typeof(callbackData));
console.log("creating --------------------> " + typeof(callbackData1));
console.log("pushing data in"); 
     callbackData.push(callbackData1);
user3502374
  • 781
  • 1
  • 4
  • 12
  • you can't push into an object, only an array – thatOneGuy May 30 '16 at 20:05
  • that really is terrible as I need to be able to do push and shift operation in order to update the line chart in d3.js.. hmmm – user3502374 May 30 '16 at 20:14
  • D3 works with arrays of objects not objects containing objects. So this way will work fine – thatOneGuy May 30 '16 at 20:16
  • I guess I really just don't understand this thing too well. callbackData is an object w/ structure like [ object,object,object,object]. Why can't I just push in another object? It seems like I can shift things from it but not push?(that is push another object and not an array). – user3502374 May 30 '16 at 20:28
  • Your callbackData in that instance is not an object, it is an array. I know this because the square brackets represent array ([ ]), where as an object is curly braces ({ }). So you can use callbackData.push(object to push here) – thatOneGuy May 30 '16 at 20:31
  • but when I do typeof(callbackData), I get back "object" – user3502374 May 30 '16 at 20:34
  • Check here : http://stackoverflow.com/questions/12996871/why-does-typeof-array-with-objects-return-object-and-not-array or here : http://stackoverflow.com/questions/4775722/check-if-object-is-array – thatOneGuy May 30 '16 at 20:36
  • Okay, so I don't know if that is terribly good news(array is actually type of an object, which means I cannot truly find out if something is pure array?). But then okay, now I am going back to my original problem. callbackData is an array which contains objects. I cannot push another object into this array? I am so sorry for going around and around but I just haven't found the right explanation. – user3502374 May 30 '16 at 20:45
  • yes you can push as callbackData is an array not an object. So callbackData.push(placeObjectToPushHere) will work – thatOneGuy May 30 '16 at 20:46
  • I will try it again but when I do that, I got callbackData = ["object',"object","object","array"] when I want newly created object that I just pushed in to go in as "object". – user3502374 May 30 '16 at 20:49
  • show example code, preferably get a JSFiddle together and ill take a look – thatOneGuy May 30 '16 at 20:51
  • Data gets read from mysql https://jsfiddle.net/askingquestionsagain/nr6ztspf/ – user3502374 May 30 '16 at 20:56
  • Can you not just mock it up ? – thatOneGuy May 30 '16 at 20:56
  • will try right now.. thanks for your help – user3502374 May 30 '16 at 20:58
  • my mockup sounds like it should work but my data generation and assigning to data is not working the way it should when I use d3.json – user3502374 May 30 '16 at 21:44
  • sorry, just cannot convert d3.json feed right into the html page. I will just have to work on this w/ real data.. really appreciate your help though! – user3502374 May 30 '16 at 23:03
  • I am not crazy about this but I was able to get around this particular problem by converting the array into object. Still not updating properly but this particular issue is done now. for ( var i = 0 ; i < mycallbackData1Length; i++){ callbackData.push(callbackData1[i]); } – user3502374 May 30 '16 at 23:34

1 Answers1

0

Try using .concat() instead of .push(), as it sounds like you need to join two arrays-of-objects.

http://www.w3schools.com/jsref/jsref_concat_array.asp

Specifically, try:

callbackData = callbackData.concat(callbackData1);

Instead of:

callbackData.push(callbackData1);
wdickerson
  • 849
  • 7
  • 8
  • This actually looks like it did push the object into the array(so that is success in that way) but overall is still not working. but thank you for this idea! – user3502374 May 30 '16 at 21:45