admin管理员组文章数量:1356873
On my node.js server I have the following code:
var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);
I expected the console to say this:
tags: [{"value":"tag1"},{"value":"tag2"}]
But instead got this:
tags: [object Object],[object Object]
Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.
On my node.js server I have the following code:
var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);
I expected the console to say this:
tags: [{"value":"tag1"},{"value":"tag2"}]
But instead got this:
tags: [object Object],[object Object]
Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.
Share Improve this question edited Mar 7, 2019 at 2:00 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Jan 2, 2019 at 6:14 Cole TaylorCole Taylor 736 bronze badges 2-
1
using the
+
operator to concatenate an object with a string is like calling'string' + obj.toString()
, which({}).toString()
is[object Object]
. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather[object Object]
. – user5201343 Commented Jan 2, 2019 at 6:20 - If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result. – Komal Bansal Commented Jan 2, 2019 at 6:46
6 Answers
Reset to default 5You have two options:
1: Use the ma ,
instead of concatenating the strings together, to avoid toString()
being called and creating [object Object]
:
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", tags);
2: Use JSON.stringify()
on the object to convert it into a string which can be read:
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", JSON.stringify(tags));
When you do "tags: " + tags
, the toString
method of objects is called in order to do the operation.
Change
console.log("tags: " + tags);
into
console.log("tags: ", tags);
so that the console.log
function of node can do its own more interesting conversion.
You can also use JSON
to log Objects properly if you want to concatenate the strings.
var tags = [{
"value": "tag1"
}, {
"value": "tag2"
}];
console.log("tags: " + JSON.stringify(tags))
When you create a concatenated string using the +
operator, the .toString()
method is called on the non-string parts to convert them to readable strings – and this method returns [object Object]
for plain objects.
If you want to see the actual content of the array, use :
console.log("tags: ", tags);
(when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;- or
console.log("tags: " + JSON.stringify(tags));
if you just want to see the content of the array printed (useJSON.stringify(tags, null, 2)
for pretty print with 2-spaces indent).
Why does this happen?
It happens because when you try to concatenate any variable with a string using +
operator, javascript converts the value of the variable to a string.
'+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".
本文标签: javascriptWhy does quotvaluequotquottag1quot turns into object Object when loggedStack Overflow
版权声明:本文标题:javascript - Why does [{"value":"tag1"} turns into [object Object] when logged? - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743956785a2568275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论