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)