我希望在屏幕上显示pushIssue
数组,而对象issue
仅在Javascript的帮助下推送到数组pushIssue
。根据我的知识,可以使用pushIssue.push(issue)
完成,然后我尝试使用document.write()
在屏幕上显示它但无法显示
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 = [];
答案 0 :(得分:2)
将元素推送到数组后,您可以使用JSON.stringify
JSON.stringify()
方法将JavaScript值转换为JSON字符串,如果指定了replacer函数,则可以选择替换值,或者如果指定了replacer数组,则可选地仅包括指定的属性。
表示数组的JSON notation中的字符串化值。
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>');
为了获得更好的解决方案,因为如果页面已经呈现并且document.write
生成新页面,您可以使用<pre>
标记并插入字符串化对象。
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>
答案 1 :(得分:-1)
使用pushIssue.toString()而不是pushIssue。