Nima Azii
Nima Azii

Reputation: 47

Print the array as the array itself

I have an array and I want to print this array as an array itself in a div like this:

array = [[1,2],[1,3],[1,4]]

Here is what I have currently:

var a = [[1,2],[1,3],[1,4]];


document.querySelector("#test").innerHTML= "array = " + a
<div id="test"></div>

Upvotes: 1

Views: 49

Answers (2)

Rishabh Jain
Rishabh Jain

Reputation: 108

You should use JSON.stringify for this and you should also use pre tag in addition to it to make it look good

const json = [[1,2],[1,3],[1,4]];


document.querySelector("#json").innerHTML= "<pre>" + JSON.stringify(json, null, 4) + "</pre>"
<div id="json"></div>

Upvotes: 0

Mark Skelton
Mark Skelton

Reputation: 3891

You can use JSON.stringify to produce the result you are expecting.

var a = [[1,2],[1,3],[1,4]];


document.querySelector("#test").innerHTML= "array = " + JSON.stringify(a)
<div id="test"></div>

Upvotes: 1

Related Questions