I have this object which has some symbols properties on it:
{
foo: 'bar',
[Symbol(raw.json.bytes)]: 13,
[Symbol(raw.json.str)]: '{"foo":"bar"}'
}
now, I added those symbol properties myself, so maybe I can change how I add them using Object.defineProperty. Is there some way to prevent logging the symbols either:
Either way I am using util.inspect() to stringify the object, and my preference is to pass it an option to not log non-enumerable properties or what not.
You can do it if the showHidden option is set to false (default) by using non-enumerable symbols.
To do this, define symbols the following way:
Object.defineProperty(obj, symbol, {
enumerable: false, //that matters!
configurable: true, //or false
writable: true, //or false
value: value
})
Properties defined like this (not necessarily symbols1) will not be included in util.inspect calls if the showHidden option is false.
1: Defining non-enumerable non-symbol properties has the side effect of skipping them when iterating over the keys/values of an object with for..in loops, Object.keys() or Object.values(). Read more on MDN or in this SO post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With