Javascript: How to print object line by line?

499 Views Asked by At

In my React Native app I'm displaying modals that contain a printout of an object. I use the Alert.alert() method, which you need to pass a string to. Since I have an object I want to print, I pass JSON.stringify(obj) to the method. However, this prints the object like this:

{"prop1":"value1","prop2":"value2"}

Is there a way to print it like

{
  prop1: value1,
  prop2: value2
}

without looping through the keys of the object?

1

There are 1 best solutions below

3
Alon Shmiel On

Run it in the console. It will copy it to your clipboard.

const beautifyJSON = JSON.stringify({"prop1":"value1","prop2":"value2"}, null, "\t");

You can read about stringify parameters here