26

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 .

Organizations is [object Object]

How do i get all the content of organizations . I did this in my code for console .

console.log('Organizations is '+organizations);
3

7 Answers 7

67

You can stringify it.

console.log(JSON.stringify(organizations));

if you want some formatting

console.log(JSON.stringify(organizations, null, 2));
1
  • 2
    This should definitely be the accepted answer; the pretty-printing (second code block) is exactly what I was looking for when having the same issue. +1 Jan 13, 2019 at 14:41
10

Here are all methods to print an object without going mad. Print object in JavaScript

console.log('Organisations is : ' + JSON.stringify(organisations));
2
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. See here for instructions how to write better "link-based" answers. Thanks!
    – GhostCat
    Aug 30, 2018 at 7:14
  • added essentials info related to answer. Aug 30, 2018 at 7:49
8

In ES6 syntaxe you can do something like this :

console.log(`Organisations is : ${JSON.stringify(organisations)}`);

7

Most consoles look at the argument they're passed and show an intelligent rendering of it, so you may want to provide organizations directly rather than concatenating it with a string (which will invoke the object's default toString behavior, which is "[object Object]" if you haven't done something special). Most also support multiple arguments, so you can do

console.log("Organizations is", organizations);

...to see both your label and the intelligent rendering.

See also this question's answers about console rendering, though.

2
  • this is the best answer in that it doesn't require an additional dependency (e.g. JSON)
    – gabe
    Feb 16, 2020 at 5:50
  • This is by far the best answer. It's simple to implement and actually auto formats the data and presents it in a logical, sensical way that clearly demonstrates the structure of the data. Mar 11, 2022 at 10:26
1

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:

console.log(JSON.stringify(organizations));
0

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.

-4

use below code to print object in react-native

<View> {(()=>{ console.log(object) })()} </View>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.