..Javascript question..

Started by Naughty Alien, June 20, 2018, 05:42:57

Previous topic - Next topic

Naughty Alien

..hi guys..if i have an array created like this..


myModel.nodeDataArray =
      [
       { key: "Alpha" },
       { key: "Beta" },
       { key: "Gamma" },
       { key: "Delta" }
      ];


..so i can access individual elements like myModel.nodeDataArray[0].key, myModel.nodeDataArray[1].key, etc..

..now, how can i add new key (similar to myModel.nodeDataArray.push(), but this doesnt work in this case) at any point as i dont know in advance all keys, so i have to be able to do it at any time in runtime??


Matty

What you've done is different than how I do it but it looks like the same result mostly....

The way I do it is as follows:


var myarray = new Array();
myarray[0] = new Object();
myarray[0].key = 'Alpha';
myarray[1] = new Object();
myarray[1].key = 'Beta';

so to create more elements if you have an array like that:

myModel.nodeDataArray[4] = new Object();
myModel.nodeDataArray[4].key = 'Letter E In Greek'

Naughty Alien

..I did this, but it doesn't work (there is no error thrown either)..mm


myModel.nodeDataArray =
      [
       { key: "Alpha" },
       { key: "Beta" },
       { key: "Gamma" },
       { key: "Delta" }
       ];
     
      myModel.nodeDataArray.length=myModel.nodeDataArray.length+1;
      myModel.nodeDataArray[myModel.nodeDataArray.length-1]=new Object();
      myModel.nodeDataArray[myModel.nodeDataArray.length-1].key="SUPER DUPER";

Matty

#3
Try this:

(without that first line you've got)


myModel.nodeDataArray[myModel.nodeDataArray.length]=new Object();
myModel.nodeDataArray[myModel.nodeDataArray.length-1].key="SUPER DUPER";



Note in javascript to extend the length of an array just set a value (such as creating a new object in it) beyond the last element.

no need to set the 'length' simply 'use it'

Look at this for example:

http://www.mattiesgames.com/retrostrategy/renderer.js

Naughty Alien

#4
..this doesn't work as well...i mean, i can see it works if i check with alert(myModel.nodeDataArray[myModel.nodeDataArray.length-1].key), and its there, but for some reason framework doesn't see it..

..this may help, so you can take a look..im using GoJS in order to make graph for application im building, so this part of the code should be creating 4 individual text which could be linked then with lines(arrows), moved, and so on and then 5th element added on the way we discuss here...

Matty

Okay Mr Naughty Alien...

The array part works - we can see that with the alert mechanism...so it goes in fine.
But...I don't know GoJS - what I'm guessing is that it is storing a reference to the length of the array somewhere and then using that which is not being updated...

eg what I imagine it is doing is something like this:

'var refarraylength = 4'
then you're adding a number of elements to the array...
and in the for loop the goJS is going
for(var i=0;i<refarraylength;i++)

and it's not aware of the full length...

So there must be a variable that needs to be set that it is using internally to know the length.

Naughty Alien

..it appears to be handled by method addNodeData, and not directly as i wanted to..now it works by myModel.addNodeData({ key: "SUPER DUPER" });

Thank you for assistance man.. cheers :)