SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding item to array in JSON

    hey
    how r u ?

    i have this array:

    PHP Code:
    var J={ "myMenu" : [   
    "title"" ",  
    "popMenu":[
              {
    "title":" ""popMenu":[
                            {
    "title":" "}
                             ]
    }
              ]
    },
    ]}; 
    and i wanna to add more items in special values , but doesn't work


    PHP Code:
    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 ?

  2. #2
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    http://jsfiddle.net/u3fzQ/
    Code:
    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.

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey
    thank u for your helping
    but i have the same problem , but with different code :'(

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


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

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

    function addMenuItem(title,arr) {

    arr.push({

    "title"title,

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

    });

    }

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



    var 
    d=1texItem_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);
            }
        } 

  4. #4
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THank u

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how can i do it ,,, i need help

  7. #7
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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});

    }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •