0

How to alert a java script object in its string representation format ? For example, If there is a variable like this :

var a = {1:"abc",2:"xyz"};

How it can be printed out like below format using alert(a) or something like that ?

1 : abc
2 : xyz
1

2 Answers 2

1

JSON.stringify will convert your javascript object to String. Then your can replace "," with "\n" to show each field in new line. If you want to remove "{" then you can do .replace("{","")

var a ={x:"sdfd"}
alert(JSON.stringify(a).replace(",","\n"));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this:

var a = {1:"abc",2:"xyz"};
var s = "";
for(var i in a){
  s = s + "\n" + i + ":" + " " + a[i];
}
alert(s);

Or

alert(JSON.stringify(a));

If you want to debug objects in javascript, you must see to console.log )) try it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.