最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How To Write Object To Document with document.write() - Stack Overflow

programmeradmin0浏览0评论

I have a simple app that counts repeating numbers inside of an array. Users enter numbers through prompt (i.e. 2, 1, 2, 2).

And when I want to output the result (which is an object) through document.write() function I get: [object Object]

When I output it through console I get the result I expect: Object {2: 3, 1:1}

Down below is html and js, and Plunkr can be found here

I would like the results of the object to be written on the page...

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--    JAVASCRIPT AND JQUERY STUFF -->
<script src=".2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous">

</script>
<script src=".3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">

</script>
<script src="number-list.js"></script>
</head>
<body>
<h1>Enter numbers when prompted!</h1>
<p>Here are the numbers entered on the left and frequency of occurence on the right.</p>
</body>
</html>

JAVASCRIPT

$(document).ready(function(){
var n = prompt("Enter your numbers").split(",");
console.log(n);
var counts = {};
n.forEach(function(x){ 
counts[x] = (counts[x] || 0) +1; 
});
console.log(counts);
document.write(
"<h1>Enter numbers when prompted!</h1>" +
"<p>You entered following numbers:</p>" + n + "<br/>" +
"<p>The occurence is as follows:</p>" + counts);
});

I have a simple app that counts repeating numbers inside of an array. Users enter numbers through prompt (i.e. 2, 1, 2, 2).

And when I want to output the result (which is an object) through document.write() function I get: [object Object]

When I output it through console I get the result I expect: Object {2: 3, 1:1}

Down below is html and js, and Plunkr can be found here

I would like the results of the object to be written on the page...

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--    JAVASCRIPT AND JQUERY STUFF -->
<script src="https://code.jquery./jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous">

</script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">

</script>
<script src="number-list.js"></script>
</head>
<body>
<h1>Enter numbers when prompted!</h1>
<p>Here are the numbers entered on the left and frequency of occurence on the right.</p>
</body>
</html>

JAVASCRIPT

$(document).ready(function(){
var n = prompt("Enter your numbers").split(",");
console.log(n);
var counts = {};
n.forEach(function(x){ 
counts[x] = (counts[x] || 0) +1; 
});
console.log(counts);
document.write(
"<h1>Enter numbers when prompted!</h1>" +
"<p>You entered following numbers:</p>" + n + "<br/>" +
"<p>The occurence is as follows:</p>" + counts);
});
Share Improve this question asked Sep 4, 2017 at 11:35 priusprius 1131 gold badge4 silver badges12 bronze badges 4
  • You can't. Because HTML itself can't parse objects. You can serialize it to a string though. – Red Commented Sep 4, 2017 at 11:37
  • I tried using String(counts) but it didn't work. JSON.stringify(counts) did the trick however. Thanks a bunch you guys. – prius Commented Sep 4, 2017 at 11:47
  • 1 string(counts) (does that function even exist?) is wrong. JSON.stringify is correct. See my awnser with the fiddle. – Red Commented Sep 4, 2017 at 11:47
  • Well, probably not :)... Yup.. I did it your way and it works beautifully.. Thanks – prius Commented Sep 4, 2017 at 19:11
Add a ment  | 

3 Answers 3

Reset to default 1

You can't. HTML can't parse objects. You can however convert the object to a string

$(document).ready(function(){
var n = prompt("Enter your numbers").split(",");
console.log(n);
var counts = {};
n.forEach(function(x){ 
counts[x] = (counts[x] || 0) +1; 
});
console.log(counts);
document.write(
"<h1>Enter numbers when prompted!</h1>" +
"<p>You entered following numbers:</p>" + JSON.stringify(n) + "<br/>" +
"<p>The occurence is as follows:</p>" + JSON.stringify(counts));
});
<script src="https://code.jquery./jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous">

</script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">

</script>
<script src="number-list.js"></script>
<body>
<h1>Enter numbers when prompted!</h1>
<p>Here are the numbers entered on the left and frequency of occurence on the right.</p>

<div id="content"></div>

Use JSON.stringify(counts) to serialize you object and you can print it in the HTML

document.write(
"<h1>Enter numbers when prompted!</h1>" +
"<p>You entered following numbers:</p>" + n + "<br/>" +
"<p>The occurence is as follows:</p>" + JSON.stringify(counts));
});

You can convert the object to string using JSON.stringify(counts)

$(document).ready(function(){
  var n = prompt("Enter your numbers").split(",");
  console.log(n);
  var counts = {};
  n.forEach(function(x){ 
    counts[x] = (counts[x] || 0) +1; 
  });
  console.log(counts);
  document.write(
  "<h1>Enter numbers when prompted!</h1>" +
  "<p>You entered following numbers:</p>" + n + "<br/>" +
  "<p>The occurence is as follows:</p>" + counts);
});
发布评论

评论列表(0)

  1. 暂无评论