We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am very new to this and could use a point in the direction of where to find more information on how divide up and create different arrays in a flexible way. thanks for any help!
I am using government json files to make files to cut on a cnc. the issues is very simple and I know what causes it I just need help finding some information on how to deal with it and maybe even a work around.
here is my issue: Some objects will have 30 items and others will have 35 items and I am not sure how to deal with the issue of missing data that i am looking for. If I for-loop through the array of objects and look at each item for some info and its not there I get an error. ----> can not read property "whatEverImLookingFor" of undefined.
I have seen the files in column format and the columns will be blank. I want to know how to make it blank or just omit those objects missing the info I am looking for.
sorry If this does not make sense I am not even sure what to call the things I am describing for instance is this what everyone is calling parsing. I need just a really good push in the correct direction thanks! -t
Answers
It's pretty generic what you write
To show some code and example data would help
But from what you wrote, you should use some kind of jsonArray.size() in the for loop as a upper bound to avoid that you read over the end of an json array.
Now if you fill your own array with a fixed size in this for loop and you pre-define the content as 0 (or it is automatically 0) you have default values at the end that don't get overwritten in the for loop. (Or use an array of ArrayList of different sizes).
Second idea: if you retrieve data that's not there the returned value in some cases is null (this is for loadStrings(), not tested for json). So you can test on if(val!=null) etc. - see reference null
You could combine this with a try catch clause, see reference. After the clause when there was an error val should be null. So you can test this.
As said, all very generic.
Chrisir
i just made a quick sketch to demonstrate the problem. I hope i posted it correctly first time so please let me know if I should be doing it differently. makePoint illustrates what data I want, makePoints illustrates how it gets hung up if the data is missing. thanks for your previous response I just wanted to give an example to be more specific .
after re-reading your post (5 times) Chrisir i see the magic in your setup. it would allow me to keep moving though the data. Then I can look to where the info SHOULD be in the array and then conditional out if it does not look correct. This seems like a complicated fix but maybe this is the result of working with half filled data fields. I guess I was hoping for a cheat-code object, this seems like the porpper solution, thanks for your timley response I will fiddle with it and post an update later today! -t
Please post also some 20 lines of data where the irregularities show up
Is there any reason you can't simply check the length of the received array before iterating over it? Or is the issue that the returned array is always the same length but some fields are empty?
In the latter case JS offers some useful workarounds to avoid errors being thrown:
Alternatively you can do without the default fallback and check whether lat is truthy before doing anything with it.
Note that this is a simplified example: you also need to be aware of JS idiosyncrasies related to testing truthiness of variables, particularly 0! In the above example a returned co-ordinate of 0 will resolve to the default :/
blindfish I just plugged in what you said and it works, again after reading this multiple times the answer is digested and mostly understood. cant thank you both enough. blindfish i honestly would have never put an or there but what a great trick this seems to be the simple work around I was missing. I will definitely post with code with my question in the future thanks chrisir, I see why its needed and thanks for the bigger overview of how it could be handled.
any book that is a must read? or just keep plugin along and asking questions. cheers! -t
;-)
@tLap - re: books...
It's a little dated now but Crockfords "Javascript: the Good Parts" gives some insight into some common JS gotchas (here's a slideshow summary).
I haven't been especially active on the forums lately - but I usually recommend the You don't know JS series; not least as it's free to read online and encourages exploration of JS's functional programming potential; rather than saddling you with all that OOP heaviness.
thank you, thank you, thank you! just what i am looking for.