javascript 如何在 React Native 的控制台中显示整个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50904654/
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
how to display the entire object in console in react native
提问by satyajeet jha
I recently started doing react native and don't understand as how to debug so as to get my results in console . Here is the result I get in console .
我最近开始做 react native 并且不明白如何调试以便在 console 中获得我的结果。这是我在控制台中得到的结果。
Organizations is [object Object]
How do i get all the content of organizations . I did this in my code for console .
我如何获得组织的所有内容。我在我的 console 代码中做到了这一点。
console.log('Organizations is '+organizations);
回答by agenthunt
回答by patidarsnju
Here are all methods to print an object without going mad. Print object in JavaScript
以下是打印对象而不会发疯的所有方法。JavaScript 中的打印对象
console.log('Organisations is : ' + JSON.stringify(organisations));
回答by T.J. Crowder
Most consoles look at the argument they're passed and show an intelligent rendering of it, so you may want to provide organizationsdirectly rather than concatenating it with a string (which will invoke the object's default toStringbehavior, which is "[object Object]"if you haven't done something special). Most also support multiple arguments, so you can do
大多数控制台查看它们传递的参数并显示它的智能渲染,因此您可能希望organizations直接提供而不是将其与字符串连接(这将调用对象的默认toString行为,即"[object Object]"如果您没有做某事特别的)。大多数还支持多个参数,所以你可以这样做
console.log("Organizations is", organizations);
...to see both your label and the intelligent rendering.
...查看您的标签和智能渲染。
See also this question's answersabout console rendering, though.
回答by Pascal Ruscher
If you try to log with a string the console tries to convert your object to a string definition automatically.
如果您尝试使用字符串登录,控制台会尝试自动将您的对象转换为字符串定义。
So either you log the string separately:
所以要么单独记录字符串:
console.log('Organizations is');
console.log(organizations);
Or you need to convert your object to a readable format first e.g. JSON:
或者您需要先将对象转换为可读格式,例如 JSON:
console.log(JSON.stringify(organizations));
回答by Isaac
let organizations = {name: 'McD'}
console.log(organizations)//Without type coercion
console.log('organizations is '+organizations);//Forcefully to convert object to become a string
The problem with console.log('Organizations is '+organizations);is due to type coercion. You are combining/concatenating a string ('Organizations is ') with an object(organizations) which forcing to convert an object into a string.
问题console.log('Organizations is '+organizations);是由于类型强制。您正在将一个字符串('Organizations is ')与一个对象(organizations)组合/连接,该对象强制将一个对象转换为一个字符串。
回答by LaxmiKant Prajapati
use below code to print object in react-native
使用下面的代码在 react-native 中打印对象
<View> {(()=>{ console.log(object) })()} </View>

