Aseem Sharma
Aseem Sharma

Reputation: 149

Javascript push method to populate array

I want to display the pushIssue array on screen with object issue pushed into array pushIssue with help of Javascript only. According to my knowledge it can be done with pushIssue.push(issue), then I tried to display it on screen using document.write() but unable to display it

var issue = {
    id: 1,
    description: "This space is for description",
    severity: "This is severity",
    assignedTo: "Name of the assigned person",
    status: "Issue Status "
  }
var pushIssue = [];

Upvotes: 2

Views: 557

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386560

After pushing the element to the array, you could use JSON.stringify

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

for a stringified value in JSON notation of the array.

var issue = {
        id: 1,
        description: "This space is for description",
        severity: "This is severity",
        assignedTo: "Name of the assigned person",
        status: "Issue Status "
    },
    issues = [];

issues.push(issue);

document.write('<pre>' + JSON.stringify(issues, 0, 4) + '</pre>');

For a better solution, because if the page is already rendered and while document.write generates a new page, you could use an <pre> tag and insert the stringified object.

var issue = {
        id: 1,
        description: "This space is for description",
        severity: "This is severity",
        assignedTo: "Name of the assigned person",
        status: "Issue Status "
    },
    issues = [];

issues.push(issue);

document.getElementById('out').appendChild(document.createTextNode(JSON.stringify(issues, 0, 4)));
<pre id="out"></pre>

Upvotes: 2

schaturv
schaturv

Reputation: 122

Use pushIssue.toString() instead of only pushIssue.

Upvotes: -1

Related Questions