Sublime Forum

Fold Unfold Object and or Array

#1

Are there any packages that allows me to unfold an object. For example going from this:

var object = [{ key1: 'hello', key2: 'bye' }, { key1: 'your', key2: 'welcome' }];

To this and backwards:

var object = [
    {
        key1: 'hello',
        key2: 'bye'
    }, 
    {
        key1: 'your',
        key2: 'welcome'
    }
];

Thank you.

Jules

0 Likes

#2

With the caveat that I’ve never really used it, the HTML-CSS-JS-Prettify package might do what you want.

A quick install in a Sublime sandbox with all default settings (except for having to customize the path to nodejs), I was able to convert your sample code into this with a couple of keystrokes:

var object = [{
        key1: 'hello',
        key2: 'bye'
    },
    {
        key1: 'your',
        key2: 'welcome'
    }
];

In order to do that I had to first select the JSON object and trigger the command, then select the whole construct and trigger the command again; just selecting the single line didn’t re-flow it. Note also that the opening brace isn’t properly shifted to the correct location.

Possible one or both issues could be resolved by configuring things a little more than they are out of the box.

0 Likes

#3

I never found any reliable plugins for this, but it’s been a while since I looked. There are online beautifiers like https://codebeautify.org/jsviewer/, those tend to work.

0 Likes