When utilizing the code snippet below:
const _ = require("lodash");
const mongoose = require("mongoose");
const objectId = new mongoose.Types.ObjectId('deadcaffee00deadbeef1122');
const isEmpty = _.isEmpty(objectId);
console.log("keys", Object.keys(objectId));
for (let i in objectId) {
console.log("property", i, objectId[i]);
}
console.log({ objectId, isEmpty });
It becomes evident that the objectId
variable only possesses one property identified by a Symbol
.
Observing closely, this property is not shown as we log it (only valueOf
gets logged), and the keys array turns out to be empty.
A look at the underlying source code reveals a similar iteration of keys using a for-in loop, mirroring my approach to logging properties.
As there are no suitable properties to satisfy hasOwnProperty
, the loop concludes with a return value of true
.