1

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>

4
  • @MarkSkelton why change the congenial tone of the original by changing, "Friends, how should I do this?" Just because that's not how you write/speak does not mean it must be changed. Nothing is gained by that edit and personally I appreciated the friendly tone of the unedited question. Commented Jan 2, 2021 at 17:48
  • @Paul As I understand, extra fluff in questions (e.g. Hi, Thanks, etc.) is not something that Stack Overflow encourages. This post on meta has some more info about this: meta.stackexchange.com/questions/2950/…. I recognize that it is talking more about the start and end of a post, but I think the principles still apply. Commented Jan 2, 2021 at 17:57
  • @MarkSkelton You're not wrong but if OP had written "How should I do this?" would it need to be changed? What about "JavaScript experts, how should I do this?"? Those are rhetorical questions to illustrate how I see the phraseology. I agree that starting the question with "Hello, friends!" would be considered "fluff" but given it is a new user's first question I would allow more latitude even in that case and instead leave a comment saying, "Welcome to SO! In future questions...". We can agree to disagree, it's not a big deal. Happy New Year! Commented Jan 2, 2021 at 18:08
  • 1
    @Paul You definitely make good points and I appreciate your thoughts and opinions. I'll definitely keep this in mind as I edit future questions. Happy New Year to you as well! Commented Jan 2, 2021 at 18:11

2 Answers 2

1

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>

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

Comments

0

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>

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.