Mas Bagol
Mas Bagol

Reputation: 4617

How to specify which property to display when doing console.log to object?

When creating an object from constructor, sometimes the result object have too much property to display when using console.log. How can I specify which property to display?

For example if I have a constructor like this:

function Obj(source) {
  this.lines = source.split('\n'); // it can be 1000 lines or even more
}

So, If I do:

var obj = new Obj(source); console.log(obj);

it will print all those lines to the console. I want to exclude that property on console.log, how?

Upvotes: 0

Views: 1302

Answers (5)

iamalismith
iamalismith

Reputation: 1571

If it's just in the logging that you want to omit the property you could define and immediately call a function in your console.log command.

The function could copy the object, remove the lines property from it and log the resulting object:

function Obj(source) {
  this.lines = source.split('\n'); // it can be 1000 lines or even more
}
var obj = new Obj(source); 
console.log((function(){var copy = Object.assign({}, obj); delete copy.lines; return copy;})())

Upvotes: 0

Bergi
Bergi

Reputation: 664538

On nodejs, console.log does use util.inspect to format objects for printing. You can customise it by providing your own inspect method on your objects.
Another simple way to prevent a property from being printed to the node console is to make it non-enumerable, as long as that doesn't break your code.

In browsers, with their interactive inspection of logged objects, you usually don't have a problem with too-large objects, as they will be expanded only on request. If you want to control exactly what is printed, your only option is to pass a string to console.log, though.

Upvotes: 1

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Add a method which will print the object as you like. E.g.

Obj.prototype.debug = function () {
  console.log({
    x: this.x,
    y: this.y
  });
};

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

It will by default print all the properties. Unless you specify which property you want to print, it will print all.

If you don't want all the lines then define the property as a function which will give line by line view

function Obj(source) {
  this.source = source;
  this.lines = function(){
     return this.source.split('\n');
  } 
}

Now if you do console.log(Obj) it will only show one line.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074295

Other than using something else, no, you can't reasonably limit what console.log shows, and what it shows varies from console implementation to console implementation (or even within the same implementation depending on whether you're showing the console or not when log is called).

You may find console.dir more useful in this case than console.log. From MDN:

Displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

Of course, that's only really useful if you have an interactive console display (such as in a browser).

Alternately, you could use console.log(String(obj)); and override the default toString to do what you want:

Obj.prototype.toString = function() {
    // generate your designer output here
    return desiredOutputString;
};

Depending on the console implementation, in some cases, you may not need the String(...) part when calling console.log, but in most of them you would.

Upvotes: 0

Related Questions