DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Prevent an object key from appearing in `Object.keys()` or `for..in` loop

const obj = { name: "Human", age: 26, location: "World", role: "Developer" };

console.log(Object.keys(obj)); // [ 'name', 'age', 'location', 'role' ]

// Updating setting for `name` property 
Object.defineProperty(obj, "name", {
  enumerable: false,
});

console.log(Object.keys(obj)); // [ 'age', 'location', 'role' ]

const objKeys = [];
for (const key in obj) objKeys.push(key);

console.log(objKeys); // [ 'age', 'location', 'role' ]
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ๐Ÿ’™

Follow @codedrops.tech for daily posts.

Instagram โ— Twitter โ— Facebook

Micro-Learning โ— Web Development โ— Javascript โ— MERN stack โ— Javascript

codedrops.tech

Top comments (0)