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 47941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:12:06+00:00 2026-05-10T16:12:06+00:00

I’m trying to find a way to "pretty print" a JavaScript data structure in

  • 0

I’m trying to find a way to "pretty print" a JavaScript data structure in a human-readable form for debugging.

I have a rather big and complicated data structure being stored in JS and I need to write some code to manipulate it. In order to work out what I’m doing and where I’m going wrong, what I really need is to be able to see the data structure in its entirety, and update it whenever I make changes through the UI.

All of this stuff I can handle myself, apart from finding a nice way to dump a JavaScript data structure to a human-readable string. JSON would do, but it really needs to be nicely formatted and indented. I’d usually use Firebug’s excellent DOM dumping stuff for this, but I really need to be able to see the entire structure at once, which doesn’t seem to be possible in Firebug.

  • 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. 2026-05-10T16:12:07+00:00Added an answer on May 10, 2026 at 4:12 pm

    I wrote a function to dump a JS object in a readable form, although the output isn’t indented, but it shouldn’t be too hard to add that: I made this function from one I made for Lua (which is much more complex) which handled this indentation issue.

    Here is the ‘simple’ version:

    function DumpObject(obj) {   var od = new Object;   var result = '';   var len = 0;    for (var property in obj)   {     var value = obj[property];     if (typeof value == 'string')       value = ''' + value + ''';     else if (typeof value == 'object')     {       if (value instanceof Array)       {         value = '[ ' + value + ' ]';       }       else       {         var ood = DumpObject(value);         value = '{ ' + ood.dump + ' }';       }     }     result += ''' + property + '' : ' + value + ', ';     len++;   }   od.dump = result.replace(/, $/, '');   od.len = len;    return od; } 

    I will look at improving it a bit.
    Note 1: To use it, do od = DumpObject(something) and use od.dump. Convoluted because I wanted the len value too (number of items) for another purpose. It is trivial to make the function return only the string.
    Note 2: it doesn’t handle loops in references.

    EDIT

    I made the indented version.

    function DumpObjectIndented(obj, indent) {   var result = '';   if (indent == null) indent = '';    for (var property in obj)   {     var value = obj[property];     if (typeof value == 'string')       value = ''' + value + ''';     else if (typeof value == 'object')     {       if (value instanceof Array)       {         // Just let JS convert the Array to a string!         value = '[ ' + value + ' ]';       }       else       {         // Recursive dump         // (replace '  ' by '\t' or something else if you prefer)         var od = DumpObjectIndented(value, indent + '  ');         // If you like { on the same line as the key         //value = '{\n' + od + '\n' + indent + '}';         // If you prefer { and } to be aligned         value = '\n' + indent + '{\n' + od + '\n' + indent + '}';       }     }     result += indent + ''' + property + '' : ' + value + ',\n';   }   return result.replace(/,\n$/, ''); } 

    Choose your indentation on the line with the recursive call, and you brace style by switching the commented line after this one.

    … I see you whipped up your own version, which is good. Visitors will have a choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This query should do it. The syntax might be a… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer One option is to pass in Bar.class (or whatever type… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer Update: Based on the question update, my suggestion is: Create… May 12, 2026 at 12:44 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.