Deleting .json Items || = undefined??
-
How do we delete an item from .json? My 'Rename Preset' isnt replacing the selected preset for some reason, its adding a new item. Im removing the item before writing the object back to file so im assuming the deleting isnt happening.
//-------Delete the Old Entry--------------------------- AttackTablePresets[oldName] = undefined;
This is the full 'Rename' button script as of now...
// -------------------------RENAME PRESET BUTTON CONTROLS----------------------- inline function onRenamePresetButtonControl(component, value) { if (!value) return; //------------------------------------------------------------------- local oldName = PresetList.getItemText(); // Name in combobox local newName = PresetNameToSave.get("text").trim(); // Name in text field //------------------------------------------------------------------- if (newName == "" || newName == oldName) return; //------------------------------------------------------------------- // If new name already exists, do nothing or show warning. if (isDefined(AttackTablePresets[newName])) { Console.print("A preset with that name already exists."); return; } //------------------------------------------------------------------- // Duplicate Data AttackTablePresets[newName] = AttackTablePresets[oldName]; // Copy data FROM oldName TO newName //------------------------------------------------------------------- // Rebuild the object without the old key local newPresets = {}; // Clear the array for (k in AttackTablePresets) { if (k != oldName) newPresets[k] = AttackTablePresets[k]; // Array, newPresets now without oldName } //------------------------------------------------------------------- AttackTablePresets = newPresets.clone(); // Hand back to main array AttackTablePresets //------------------------------------------------------------------- // Get current values local tableData0 = TableEnvelope1Pro.exportAsBase64(0); // ATTACK TABLE local attackKnob = knbAttack.getValue(); // ATTACK TIME KNOB AttackTablePresets[newName] = [ TableEnvelope1Pro.exportAsBase64(0), knbAttack.getValue() ]; //-------Delete the Old Entry--------------------------- AttackTablePresets[oldName] = undefined; //------------------------------------------------------------------- // Write object to file presetFile1.writeObject(AttackTablePresets); //------------------------------------------------------- // Update ComboBox local keyList = []; for (k in AttackTablePresets) keyList.push(k); //------------------------------------------------------------------- // Repopulate combobox items and set new value PresetList.set("items", keyList.join("\n")); PresetList.setValue(keyList.indexOf(newName) +1); //------------------------------------------------------------------- PresetNameToSave.set("text", ""); }; RenamePresetButton.setControlCallback(onRenamePresetButtonControl);
.json in xcode after trying to rename "1" to "11"
then my preset combobox has some rogue values. The .json doesnt show those values so idk.
-
The usual way is to set it to undefined, however very recently the topic came up and Christoph mentioned adding a proper remove function. I'm not sure if it was implemented though.
How can I efficiently remove a key from an object?
@d-healey yeah something like JSON.delete(obj, key) is the easiest way, the delete syntax would require a real brain-twister with the current parser.
Forum (forum.hise.audio)
-
@d-healey darn, so even if he did it would require me to update which Im afraid to do. Any other suggestions?
-
@Chazrox said in Deleting .json Items || = undefined??:
Any other suggestions?
In that same thread there's an example function for removing an item from an object by rebuilding the object.
-
@d-healey If its writing the new data, and not deleting the old one...where are these numbers coming from?
they look like items i've added and removed before but if they arent showing up in the .json file, where are they?