0

I working with arrays today and am now absolutely exhausted (understatement). For some reason my arrays 'fail to update' (for want of a better word) -or at least so it seems in my console window.

The only way I can get my arrays to 'update' is to 'deep copy' them before I perform further operations, like so:

graphArray = JSON.parse(JSON.stringify(graphArray));

The only way I could get the below code to 'work' (i.e. to 'update' its values) was to deep copy the array between every single step, like so:

//supply
plugIt = graphArray['supplyDemand']['supply'][ind];
graphArray['supplyDemand']['supply'][ind] = parseInt(plugIt) + parseInt(total);

graphArray = JSON.parse(JSON.stringify(graphArray));

//cardiology
plugIt = graphArray['supplyDemand']['cardiology'][ind];
graphArray['supplyDemand']['cardiology'][ind] = parseInt(plugIt) + val.cardiology ;

graphArray = JSON.parse(JSON.stringify(graphArray));

//elderly care
plugIt = graphArray['supplyDemand']['elderly care'][ind];
graphArray['supplyDemand']['elderly care'][ind] = parseInt(plugIt) + val['elderly care'];

graphArray = JSON.parse(JSON.stringify(graphArray));

Can someone please tell me what is going on here?

Noobster
  • 1,024
  • 1
  • 13
  • 28
  • 1
    Huh? Where's the "update" code? Where's the array being created? Where does it come from? Does the array even exist in that scope? What does `console.log(graphArray)` show? What does `plugIt` and `total` have? Why `parseInt` without base 10? Where is `ind`? What events are using the "array" (in reality, it is an object, not an array)? – Ismael Miguel Feb 12 '16 at 09:32
  • Also as far as I understand JSON.parse(JSON.stringify(graphArray)) should be the original graphArray – bwright Feb 12 '16 at 09:33
  • @bwright True. If it "doesn't update" before, how come it "updates" after fetching the last version of it? – Ismael Miguel Feb 12 '16 at 09:34
  • could you add more details, I am not really clear what you want to achieve here ? Do you mean that you can't change value of an item in your array or what ? – loveNZ Feb 12 '16 at 09:35
  • 1
    I think the *at least so it seems in my console window* is key here: it may seem that an object logged into console contains wrong values, because it's asynchronous and the browser gets the values when you expand the object in console and reveals the current state, not the state at the moment of logging. Try using `console.log(JSON.stringify(graphArray))` and see if it helps. – pawel Feb 12 '16 at 09:36
  • 1
    Or, try using a `setTimeout` to see if it gets updated after, say, 50 milliseconds. – Jeff Huijsmans Feb 12 '16 at 09:40
  • Pawel / Jeff thank you for coming back to me, for trying to understand my issue and for your constructive answers. I will attempt to do that and see what results I get. Ismael, with all due respect it is clear that all the operations laid out in my example attempt to modify array "graphArray" -not sure why you need all this extra detail. – Noobster Feb 12 '16 at 09:43
  • See this Plunker: https://plnkr.co/edit/BdJilDvzzMSFTD9zb0lf?p=preview as you can see all elements are updated. So maybe not everything is initialized correctly? – bwright Feb 12 '16 at 09:44
  • An array doesn't usually use string indexes... Normally it's `array[index]` where `index` is an integer. – thebearingedge Feb 12 '16 at 09:48
  • I have attempted the following: `setTimeout(function(){console.log(JSON.stringify(graphArray)),4000);`. My array still does not seem to be modified as expected. This is driving me nuts. – Noobster Feb 12 '16 at 09:51
  • `console.log` is synchronous. `JSON.stringify` is synchronous too. Can you update your question to include one example of expected results and actual results? – thebearingedge Feb 12 '16 at 09:53
  • @thebearingedge see here: https://jsfiddle.net/1o5n2eo3/1/ - at least in Chrome when you expand the logged object you'll see that `baz == 2`, even though it's been changed after `console.log` - because the console resolves the values when you expand the object and shows the current value. But the stringified logged object shows the value at the moment of logging. – pawel Feb 12 '16 at 09:56
  • @pawel. gotcha, but we still don't know the real problem :(. Chrome is just referencing the live object, so it should be working for Noobster just fine as of the last mutation. – thebearingedge Feb 12 '16 at 09:59
  • Possible duplicate of [How to parse or query complex JSON in JavaScript](http://stackoverflow.com/questions/7326405/how-to-parse-or-query-complex-json-in-javascript) – Paul Sweatte Nov 07 '16 at 15:09

0 Answers0