简体   繁体   English

使用console.log时如何隐藏对象上的某些键

[英]How to hide some keys on your object when using console.log

Okay, I'm fairly new to javascript and node so this question might sound stupid to you, but I really need an enlightenment. 好的,我对javascript和node还是很陌生,所以这个问题对您来说可能很愚蠢,但是我确实需要一个启发。 Anyway, I have an object that I got from my mongoose. 无论如何,我有一个从猫鼬那里得到的东西。 Let's make an example 让我们举个例子

Model.findOne({name:'John'}).exec(function (err, data){
 console.log(data);
});

So, when I console.loged the data the result was 因此,当我在console.log中记录数据时 ,结果是

{ _id: 613614asdfa6115, name: 'John', __v: 0 } {_id:613614asdfa6115,名称:“ John”,__ v:0}

Then, I had a stupid idea to add another key to that object, so, as a normal javascript noob would do, I add another key to the data: 然后,我有一个愚蠢的想法,向该对象添加另一个键,因此,就像普通的javascript noob所做的那样,我向数据添加了另一个键:

data.hello = 'hi' data.hello ='hi'

BUT , when I try to console.log the data the result was still the same. 但是 ,当我尝试console.log 数据时 ,结果仍然相同。 I was so baffled. 我很困惑。 So I used the lodash clone to clone the object; 所以我用lodash克隆来克隆对象。 to my surprise there were a lot of hidden keys that the first console.log did not get. 令我惊讶的是,第一个console.log没有得到很多隐藏键。

Here is the result of the cloned object: http://pastebin.com/zE71Fg2H 这是克隆对象的结果: http : //pastebin.com/zE71Fg2H

My question is: How did they hide some of the keys from the console.log? 我的问题是:它们如何隐藏console.log中的某些键?

try this - 尝试这个 -

Model.findOne({name:'John'}).exec(function (err, data){
 data = data.toObject();
 data.hello = 'hi'
 console.log(data);
});

I hope it will work. 我希望它能起作用。 For more information use this link. 有关更多信息,请使用此链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM