Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:19:12+00:00 2026-05-30T12:19:12+00:00

i need to iterate in js(no framework) over a json object generated in php

  • 0

i need to iterate in js(no framework) over a json object generated in php with the follow structure:

    {
        EDITED: I DID HAVE AN ERROR, SORRY
    }

I search for a solution without any js framework.
¿how to get, for example,

lvl1a.lvl2a.a
lvl1a.lvl2a.b
lvl1a.lvl2b.a
lvl1a.lvl2b.b
...

?

Thanks


more information:

I DID HAVE AN ERROR IN PREV CODE!!! SORRY

Function: Populate two select, “select1” and “select2”, with option=Category optgroup=CategoryParent

$toJson=array();
$selectname=array("select1","select2");
for($i=0;$i<2;$i++){
    $sql="SELECT idCategory as v,name as l,(select name from categories where idCategory=C.idCategoryParent) as p FROM categories C WHERE idBanner".($i+1)." is NULL order by idCategoryParent, `order`";
    $result =$ocdb->query($sql);
    $nameIdx = "";
    while ($row=mysql_fetch_assoc($result)){
        $first=mysql_field_name($result, 0);
        $second=mysql_field_name($result,1);
        $third= mysql_field_name($result,2);            

        if($nameIdx!=$row[$third])
                $nameIdx=$row[$third];
        }
            if($row[$third]!=NULL){
                array_pop($row);
                $toJson[$selectname[$i]][$nameIdx][]=$row;
            }

    }

}
$inJson=json_encode($toJson);

So i get something like:

    {
        "select1":  {  "optgroup1":[{"v":"1","l":"option1"},{"v":"2","l":"option2"}],
                       "optgroup2":[{"v":"3","l":"option1"},{"v":"4","l":"option2"}],
                       "optgroup3":[{"v":"5","l":"option1"},{"v":"6","l":"option2"}]
                    },
        "select2":  {  " (...)
    }

//optgroup is parentCategory
//options are categories no-parents

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:19:13+00:00Added an answer on May 30, 2026 at 12:19 pm

    Use something recursive:

    function iterateObject(obj){
      var result = [];
    
      function recurse(obj){
         for (var l in obj){
          if(obj.hasOwnProperty(l)){
           result.push( l+ 
                        (!(obj[l] instanceof Object) 
                         ? ': '+obj[l] 
                         : ' -> object >' ));
           if (obj[l] instanceof Object){
              recurse(obj[l]);
           }
          }
         }
      }
    
      recurse(obj);
      return result;
    }
    
    var thing = {
        "lvl1a":  {  "lvl2a":[{"a":"xxxxx"},{"b":"xxxxx"}],
                     "lvl2b":[{"a":"xxxxx"},{"b":"xxxxx"}],
                     "lvl2c":[{"a":"xxxxx"},{"b":"xxxxx"}]
                  },
        "lvl1b":  {  "lvl3a":[{"c":"xxxxx"},{"d":"xxxxx"}],
                     "lvl3b":[{"c":"xxxxx"},{"d":"xxxxx"}],
                     "lvl3c":[{"c":"xxxxx"},{"d":"xxxxx"}]
                  }
    };
    console.log(iterateObject(thing).join('\n'));
    //=> 
    // lvl1a -> object >
    // lvl2a -> object >
    // 0 -> object >
    // a: xxxxx
    // 1 -> object >
    // b: xxxxx
    // ... etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate over every element in a JSON object, and I'm having
I have a 3D array in Python and I need to iterate over all
I have a simple Dictionary(of String, Object) that I need to iterate through and
I have a need to iterate over all of the tables in my database.
I need to iterate over a recordset from a stored procedure and execute another
I have an table. I need to iterate, for each(datarow r in datatable.rows){ foreach(datacolumns
I have a deserialized xml c# objet. I need to iterate through the oject
I have a property within a class, that I need to iterate through all
I need to to iterate over the files in a directory and perform the
Hi I have a json string converted unsing the JSON framework into a dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.