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"}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    `alert(JSON.stringify(myKeyVals))` – Madhawa Priyashantha Nov 25 '16 at 07:44
  • Try using JSON.stringify() on the object before passing it to alert() – Gerrit0 Nov 25 '16 at 07: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. – VLAZ Nov 25 '16 at 07:47

1 Answers1

1

To get formatted output.

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

I do recommend using console.log instead of alert.

Rohith K P
  • 3,233
  • 22
  • 28