javascript 如何 console.log 对象定义和同一字符串中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30145812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:48:15  来源:igfitidea点击:

How to console.log an object definition and a text in same string?

javascriptobjectconcatenationconsole.log

提问by Ciprian Gheorghite

I have this JavaScript code:

我有这个 JavaScript 代码:

console.log(obj);// [query: "wordOfTheDay"]
console.log(note + " : " + obj ); // obj does not show up

I want to make "obj" display in the same string as "note" no matter the type it come in as.

我想让“obj”显示在与“note”相同的字符串中,无论它是什么类型。

For example:

例如:

console.log("text sample : " + obj ); // text sample : [query: "wordOfTheDay"]

Thank you!

谢谢!

回答by DACrosby

console.logaccepts any number of parameters, so just send each piece as its own param. That way you keep the formatting of the object in the console, and its all on one entry.

console.log接受任意数量的参数,因此只需将每个部分作为自己的参数发送。这样您就可以将对象的格式保留在控制台中,并将其全部放在一个条目中。

var obj = {
    query:  'wordOfTheDay',
    title:  'Frog',
    url:    '/img/picture.jpg'
};

console.log( "Text Here", obj);

// Text Here Object {query: "wordOfTheDay", title: "Frog", url: "/img/picture.jpg"}

回答by Tech Savant

you can use

您可以使用

console.log(note, obj);

回答by shershen

console.logcan take arbitrary number of arguments so you can put all data you need to log separating it by commas.

console.log可以采用任意数量的参数,因此您可以将所有需要记录的数据以逗号分隔。

console.log("text sample : ", obj, JSON.stringify(obj), (typeof obj), (new Date()))

回答by Julien Grégtheitroade

this should work:

这应该有效:

console.log(note, " : ", obj );