Currently, I am in the process of developing a webpage that involves incorporating four distinct JSON entities (objects, arrays). Excuse my lack of appropriate jargon.
Upon receiving the JSON data, it is structured as an object with numerous sub-objects, each resembling the following:
"token":"government",
"title":"Government",
"isSelected":false,
"type":"CATEGORY",
"subtype":"INDUSTRY",
"count":12
My initial task involves iterating through each JSON entity and populating a checkbox container. The title
will serve as the label and the isSelected
field will dictate the checked status. Progress has been smooth thus far.
By the way, along the way, I stumbled upon a JavaScript script that discerns whether an object is a JSON or an array. According to this "quick & dirty" evaluation, my object appears to be an array. Or rather, an array object (one created with [ ]
as opposed to { }
)?
Subsequently, once the end user toggles checkboxes, it is imperative for me to maintain a log of these changes and promptly transmit them back to the server when the user clicks on the DONE button. Strangely enough, although looping through the objects allowed me to alter the isSelected
value to true
, reverting it back to false
proved to be challenging.
for(var i = 0; i < $array.length; i++){
$array[z].isSelected = true;
}
Perhaps fatigue got the best of me while working on this project. Employing the same method, I struggled to switch $array[z].isSelected
to false
when deselecting a checkbox.
Ultimately, I resorted to converting the JSON entity into a string, conducting a search and replace operation on the relevant values, and then reconverting the string back into an object. Although this method is effective, I can't help but feel like I've patched things up with duct tape instead of assembling them seamlessly.
Question: Have I completely missed the mark? Is there a simpler way to modify values within JSON objects?
If so, could you possibly guide me toward the right solution?