Why does [{“value”:“tag1”} turns into [object Object] when logged?
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.
javascript arrays node.js string object
add a comment |
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.
javascript arrays node.js string object
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]
.
– harryparkdotio
Jan 2 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
Jan 2 at 6:46
add a comment |
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.
javascript arrays node.js string object
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.
javascript arrays node.js string object
javascript arrays node.js string object
edited Mar 7 at 2:00
Jack Bashford
12.1k31846
12.1k31846
asked Jan 2 at 6:14
Cole TaylorCole Taylor
355
355
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]
.
– harryparkdotio
Jan 2 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
Jan 2 at 6:46
add a comment |
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]
.
– harryparkdotio
Jan 2 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
Jan 2 at 6:46
1
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]
.– harryparkdotio
Jan 2 at 6:20
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]
.– harryparkdotio
Jan 2 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
Jan 2 at 6:46
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
Jan 2 at 6:46
add a comment |
6 Answers
6
active
oldest
votes
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).
add a comment |
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.
add a comment |
You have two options:
1: Use the comma ,
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));
add a comment |
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.
add a comment |
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))
add a comment |
'+' 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 ",".
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002010%2fwhy-does-valuetag1-turns-into-object-object-when-logged%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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).
add a comment |
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).
add a comment |
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).
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).
edited Jan 2 at 6:39
answered Jan 2 at 6:28
flawyteflawyte
5,02233050
5,02233050
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 2 at 6:15
Denys SéguretDenys Séguret
280k56587605
280k56587605
add a comment |
add a comment |
You have two options:
1: Use the comma ,
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));
add a comment |
You have two options:
1: Use the comma ,
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));
add a comment |
You have two options:
1: Use the comma ,
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));
You have two options:
1: Use the comma ,
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));
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", tags);
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", tags);
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", JSON.stringify(tags));
var tags = [{"value": "tag1"}, {"value": "tag2"}];
console.log("Tags: ", JSON.stringify(tags));
answered Jan 2 at 6:22
Jack BashfordJack Bashford
12.1k31846
12.1k31846
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 2 at 6:18
NickNick
3261418
3261418
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
var tags = [{
"value": "tag1"
}, {
"value": "tag2"
}];
console.log("tags: " + JSON.stringify(tags))
var tags = [{
"value": "tag1"
}, {
"value": "tag2"
}];
console.log("tags: " + JSON.stringify(tags))
edited Jan 2 at 6:22
Jack Bashford
12.1k31846
12.1k31846
answered Jan 2 at 6:17
Manash MandalManash Mandal
663
663
add a comment |
add a comment |
'+' 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 ",".
add a comment |
'+' 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 ",".
add a comment |
'+' 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 ",".
'+' 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 ",".
answered Jan 2 at 6:30
the_ultimate_developerthe_ultimate_developer
1,024722
1,024722
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002010%2fwhy-does-valuetag1-turns-into-object-object-when-logged%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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]
.– harryparkdotio
Jan 2 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
Jan 2 at 6:46