1

I have a key value pair array:

    var myKeyVals = {make: "a", model: "2"};

Is there any easy way to format this array so that it is displayed meaningful in an alert?

    alert(myKeyVals);

Here I get only:

[Object object]

But with console.log I get:

Object {make: "a", model: "2"}
3
  • 1
    alert(JSON.stringify(myKeyVals)) Commented Nov 25, 2016 at 7:44
  • Try using JSON.stringify() on the object before passing it to alert() Commented Nov 25, 2016 at 7:44
  • An even better approach - do NOT use alert. If my guess is correct - you are using this function for debugging purposes. Instead, use an actual debugger and/or console.log() statements - console.log(myKeyVals) will print exactly what you want in the browser console. Furthermore, it's way more convenient (you don't have to click OK if you have several of them) and it doesn't mess with your application - alert could mask or cause bugs. Commented Nov 25, 2016 at 7:47

1 Answer 1

1

To get formatted output.

alert( JSON.stringify(myKeyVals,null,4) ) 

I do recommend using console.log instead of alert.

Sign up to request clarification or add additional context in comments.

Comments