now the ‘john’ object have three properties (id, name, city) and one method (tellDetails) on it. Among them, we made ‘city’ as non enumerable. It has one property on its prototype (country) and one method (tellMoreDetails).
Before iterating through the array, lets do console.log(john) to check what we get.
Here is the output for console.log(john)
we can see that ‘john’ object is showing only ‘id’, ‘name’, ‘tellDetails’ and not ‘city’ as we made it non-enumerable.
Lets iterate through the Object now
1 . For – in
with the above result, we can see that we are getting ‘id’, ‘name’, ‘tellDetails’, ‘country’ and ‘tellMoreDetails’ with “for…in…” method.
Object.keys() methods returns an array of all Enumerable properties and methods of an object.
Object.keys() method does not return non-enumerable and prototype properties and methods.
Object.entries() returns an array of all Enumerable properties and methods along with their values of an object.
Similar to Object.keys(), Object.entries() also returns an array. But each array item in turn will be an array containing name value pairs.
Object.values() returns an array of values of Enumerable properties and methods of an object.
With Object.keys() we get only keys, with Object.values() we get only values and with Object.entries() we get both keys and values. All of these properties and methods must be enumerable.
Object.getOwnPropertyNames() returns an array of both Enumerable and Non-Enumerable properties and method names of an object.
For..in is the only one that is giving the names of properties and methods of an object’s prototype object.
Object.getOwnPropertyNames is the only method that is giving the names of non-enumerable properties and methods of an object.
Leave A Comment