Need assistance with manipulating/updating a JavaScript array. Hoping for some help.
The initial array looks like this:
array('saved_designs'=array());
In Javascript JSON format:
{"saved_design":{}}
I plan on adding a label and its associated array data:
array("saved_designs"=array('label'=array('class'='somecssclass',styles=array(ill add more assoc elements here),'hover'=array(ill add more assoc elements here))))
Javascript version:
{"saved_designs":{"label":{"class":"someclass","style":[],"hover":[]}}}
The goal is to be able to append/modify this array. If 'label' already exists, then update the corresponding sub-data. If 'label' doesn't exist, append a new dataset to the 'saved_designs' element.
If 'label' is not defined, add the following to the 'saved_designs' element:
array('label2' = array('class'=>'someclass2',styles=array(),'hover=>array()')
I'm having trouble understanding the usage of [] and {} in JavaScript notation.
I may need further discussion as answers are provided. Here's the code I currently have:
//saveLabel = label user chose for this "design"
if(isUnique == 0){//update
//prompt user if they want to overwrite design styles for specified html element
if (confirm("There is already a design with that label ("+saveLabel+"). Overwrite this design's data for the given element/styles?")) {
currentDesigns["saved_designs"][saveLabel]["class"] = saveClass;
//edit other subdata here...
}
}else{//create new
var newDesign = [];
newDesign[saveLabel] = [];
newDesign[saveLabel]["class"] = saveClass;
newDesign[saveLabel]["style"] = [];
newDesign[saveLabel]["hover"] = [];
currentDesigns["saved_designs"].push(newDesign);//gives error..push is not defined
}
jQuery("#'.$elementId.'").val(JSON.stringify(currentDesigns));
Thank you in advance. Hope everything is clear. Will update based on questions and comments. Shaun