obj2indent

Convert a JavaScript object to a string used the indentation.

Usage no npm install needed!

<script type="module">
  import obj2indent from 'https://cdn.skypack.dev/obj2indent';
</script>

README

obj2indent

Build Status npm version Bower License

Convert a JavaScript object to a string used the indentation.
indent2obj.js and interconvertible.

Install

npm

$ npm install obj2indent

bower

$ bower install obj2indent

Basic

  1. Download the obj2indent.min.js.
  2. Load it in the script tag.
<script type="text/javascript" src="obj2indent.min.js"></script>

Usage

var results = obj2indent([
  {
    name: "depth1",
    children: [
      {
        name: "depth2",
        children: []
      },
      {
        name: "depth2",
        children: [
          {
            name: "depth3"
          }
        ]
      }
    ]
  },
  {
    name: "depth1"
  },
  {
    name: "depth1",
    children: [
      {
        name: "depth2"
      }
    ]
  }
]);

console.log(results);
/*
depth1
  depth2
  depth2
    depth3
depth1
depth1
  dpeth2
*/

Change indent types

The default is to use 2 spaces.
If you want to the tab, Do the following.

obj2indent(input, "\t");

In the case of the 4 spaces.

obj2indent(input, "    ");

Settings

obj2indent.defaultIndent = "  ";

obj2indent.keys = {
  name: "name",
  children: "children"
};

Browser Support

  • IE6 +
  • Chrome
  • Firefox
  • Safari
  • iOS
  • Android

License

Released under the MIT Licence

Author

tsuyoshi wada