Adding item to array in JSON

hey
how r u ?

i have this array:


var J={ "myMenu" : [   
{ "title": " ",  
"popMenu":[
          {"title":" ", "popMenu":[
          			  {"title":" "}
       				  ]
}
          ]
},
]};

and i wanna to add more items in special values , but doesn’t work :frowning:


for(var b=0;b<5;b++) //i need to add 5 items in "J.myMenu[b].title"
	{
	
	J.myMenu[b].title= "tex";
      
	for(var bb=0;bb<5;bb++) // i need to add 5 items in "J.myMenu[b].popMenu[bb].title"
		{

		J.myMenu[b].popMenu[bb].title=" yahooo";
		
		alert(J.myMenu[b]. popMenu[bb].title);
		}
	}


any help plz ?

You can’t assign new values to a JavaScript array by using an index, js handles array indices internally. “Associative” arrays in javascript are not actually arrays, they are JSON. Here is a quick re-write:


var J = {
    "myMenu": []
}

function addMenuItem(title,arr) {
    arr.push({
        "title": title,
        "popMenu": []
    });
}

for(var b=0;b<5;b++) {
    addMenuItem('tex',J.myMenu);
    for(var bb=0;bb<5;bb++) {
         addMenuItem('yahoo',J.myMenu[b].popMenu);
    }
}

Probably best to look up a bit more info on JavaScript arrays and JSON. If you are used to another language (eg, PHP) the handling is slightly different and there is a distinction between the array object and object notation.

hey
thank u for your helping
but i have the same problem , but with different code :cry:

this is my code m i add extra values in JSON Array


var J={ "myMenu" : [ ]};  

//-------------------

function addMenuItem(title,arr) {

arr.push({

"title": title,

"popMenu": [{"title": title, "popMenu": [ ]
	    }]

});

}

//+++++++++++++++++++++++++++++++++++++++++++++
function creat_J()
{



var d=1, tex, Item_J,Temp_J;
for(var b=0;b<5;b++)
	{
	tex='Menu'+d;
	d++;
	addMenuItem(tex,J.myMenu[b].title);
      
	alert(J.myMenu[b].title);
	
	for(var bb=0;bb<5;bb++)
		{
alert("bbbb="+ bb);
		
addMenuItem('fun',J.myMenu[b].popMenu[bb].title);

		alert(J.myMenu[b]. popMenu[bb].title);
		}
	}

You can’t assign new values to a JavaScript array by using an index

I just realised what I’d written here, I meant to say non-numerical values, which isn’t quite what you were trying to do. Basically what you are doing wrong here is trying to work with a value before it is defined and the way you are defining it is incorrect. You don’t need to add anything extra to the popMenu array in the addMenuItem function, the previous example was adding that already. You should jump on your nearest search engine and look for some tutorials on JavaScript arrays and JSON and find something that better explains the basics I think.

THank u

:frowning:
how can i do it , i need help

thank u , it works with me when i did like this :

function addMenuItem(title,arr) {
if (flag==‘1’) {
arr.push({

“title”: title, “popMenu”: [{“title”: title,
“popMenu”: [{“title”: title}]
}]

});

}if (flag==‘2’) {

arr.push({

“title”: title, “popMenu”: [{“title”: title}]

});

}if (flag==‘0’) {

arr.push({“title”: title});

}
}