Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON Weirdness Needs More Elegant Approach
    primarykey
    data
    text
    <p>Basically, I'm working on a page that includes four different JSON "thingies" (objetcs,arrays). Forgive my lack of proper terminology.</p> <p>When I get the JSON, it comes in as an object with a bunch of sub-objects, and each "sub-object" looks like this:</p> <pre><code>"token":"government", "title":"Government", "isSelected":false, "type":"CATEGORY", "subtype":"INDUSTRY", "count":12 </code></pre> <p>So the first task is to loop through each JSON and populate a box full of checkboxes, using the <code>title</code> as the label and the <code>isSelected</code> to indicate the checked status. So far, so good.</p> <p>BTW, somewhere aslong the way, I picked up a JS script that checks whether an object is JSON or an array, and according to that "quick &amp; dirty" test, my object is an array. Or an array object (you know, the one is created with <code>[ ]</code> and the other with <code>{ }</code>)?</p> <p>Anyway, when the end user checks and un-checks checkboxes, I need to keep track of it all and immediately send back changes to the server (when the user clicks a DONE button). The crazy thing is that by looping through the objects, I was able to change the <code>isSelected</code> value to <code>true</code> . . . just not back to <code>false</code>.</p> <pre><code>for(var i = 0; i &lt; $array.length; i++){ $array[z].isSelected = true; } </code></pre> <p>Perhaps I was up too late when I worked on all of this, but using the same approach, I could <strong>not</strong> change <code>$array[z].isSelected</code> to <code>false</code> when the checkbox got de-selected.</p> <p>In the end, I converted the JSON "thingy" to a string, search and replaced the corresponding values, and then converted the string back into an object. This is all working now, but I feel as though I've just used up a roll of duct tape on something that could have been put together by snapping the pieces together nicely.</p> <p><strong>Question:</strong> Did I miss the boat totally and is there a simple way to change values of JSON objects?</p> <p>If so, could you point me in the right direction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload