27

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);
jammy
  • 857
  • 2
  • 18
  • 34

7 Answers7

70

You can stringify it.

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

if you want some formatting

console.log(JSON.stringify(organizations, null, 2));
agenthunt
  • 8,450
  • 3
  • 30
  • 26
  • 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 – James Cushing Jan 13 '19 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));
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
patidarsnju
  • 439
  • 5
  • 10
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. See [here](https://meta.stackexchange.com/a/94027/285661) for instructions how to write *better* "link-based" answers. Thanks! – GhostCat Aug 30 '18 at 07:14
  • added essentials info related to answer. – patidarsnju Aug 30 '18 at 07:49
8

In ES6 syntaxe you can do something like this :

console.log(`Organisations is : ${JSON.stringify(organisations)}`);
Eudz
  • 540
  • 6
  • 19
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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • this is the best answer in that it doesn't require an additional dependency (e.g. `JSON`) – gabe Feb 16 '20 at 05: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. – Phill Healey Mar 11 '22 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));
p0d4r14n
  • 681
  • 5
  • 15
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.

Isaac
  • 12,042
  • 16
  • 52
  • 116
-4

use below code to print object in react-native

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