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 = [];
2 Answers 2
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>
4 Comments
JSON.stringify(issues, 0, 4) if possible with an example so that I can better understand it ?stringify. basically it generates a string which looks like an object literal in javascript.Use pushIssue.toString() instead of only pushIssue.
document.write("<pre>" + JSON.stringify(pushIssue, null, 4) + "</pre>")JSON.stringify(issues, 0, 4)works !