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

  • SEARCH
  • Home
  • 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 8813779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:56:42+00:00 2026-06-14T03:56:42+00:00

In short, I have start my question, I simply read json file, [{FirstCategory:Bath,SecondCategory:Bath Accessories,ThirdCategory:},

  • 0

In short, I have start my question,

I simply read json file,

 [{"FirstCategory":"Bath","SecondCategory":"Bath Accessories","ThirdCategory":""}, {"FirstCategory":"Bath","SecondCategory":"Faucets","ThirdCategory":""},{"FirstCategory":"Bath","SecondCategory":"Fixtures","ThirdCategory":""},{"FirstCategory":"Bath","SecondCategory":"Vanities","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Concrete","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Fencing","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Gypsum","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Insulation","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Insulssdation","ThirdCategory":""}]

and trying to convert into array like

Array
(
  [Bath] => Array
    (
        [Bath Accessories] => Array
            (
                [0] => test
            )

        [Faucets] => Array
            (
                [0] => test1
                [1] => test2
            )

    )

)//sorry i have used PHP for simple formatting the array.

I spent lot of time on this stuff i can’t get success please help me.

 My javascript code : (not working.)

 var FirstCategory = [];
 var SecondCategory = [];
 var ThirdCategory = [];


 $.getJSON('tree.json', function(data) {

var dataObj = new Array();
$.each(data,function(i){
    dataObj[data[i].FirstCategory] = new Array();

    if([data[i].SecondCategory] in dataObj[data[i].FirstCategory])
        dataObj[data[i].FirstCategory][data[i].SecondCategory] = data[i].SecondCategory;
    else
        dataObj[data[i].FirstCategory][data[i].SecondCategory] = new Array();

    dataObj[data[i].FirstCategory][data[i].SecondCategory][data[i].ThirdCategory] = new Array();

});

 console.log(dataObj);

/*
$.each(data,function(i){

    if (FirstCategory == '') {
        FirstCategory.push(data[i].FirstCategory);
    }
    else
    {
        if(!FirstCategory.contains(data[i].FirstCategory))
        {
            //root
            FirstCategory.push(data[i].FirstCategory);
        }
        else
        {
            //------- second level category -------//
            if (SecondCategory == '') {
                SecondCategory.push(data[i].SecondCategory);
            }
            else
            {
                if(!SecondCategory.contains(data[i].SecondCategory))
                {
                    SecondCategory.push(data[i].SecondCategory);
                }
                else
                {
                    ThirdCategory.push(data[i].ThirdCategory);
                }
            }

        }
    }   

});
*/

});


Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
        if (this[i] == obj) {
         return true;
      }
  }
   return false;
}

Thanks in advance.

  • 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-06-14T03:56:44+00:00Added an answer on June 14, 2026 at 3:56 am

    Please notive that Javascript has no “associative arrays”.

    A bit more programmatically:

    var levels = ["FirstCategory", "SecondCategory", "ThirdCategory"]; // possibly more
    var dataObj = {}; // an empty object
    for (var i=0; i<data.length; i++) {
        var cur = dataObj;
        for (var j=0; j<levels.length; j++) {
            var key = data[i][levels[j]];
            if (!key) // empty
                break;
            if (key in cur)
                cur = cur[key];
            else
                cur = cur[key] = {};
        }
    }
    

    result (dataObj) for your example input, formatted as JSON:

    {
        "Bath": {
            "Bath Accessories": {},
            "Faucets": {},
            "Fixtures": {},
            "Vanities": {}
        },
        "Building Materials": {
            "Concrete": {},
            "Fencing": {},
            "Gypsum": {},
            "Insulation": {},
            "Insulssdation": {}
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a short version of the question: I start a thread like that:
I have a short access/mySQL question. I have a mapping table on the format
I have a short question i have wrote this in java. Old code: class
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
Short question: How do I automatically detect whether a CSV file has headers in
I have a (seemingly) simple question to read in a string and print it
Say that I have the Python code: someString = file(filename.jpg).read() How can I replicate
In Short: I have a PDF that customers can fill out. When they press
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,
I am coding a messaging system and I don't want to have short IDs

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.