7 JavaScript Object Traversal Techniques Illustrated with Allen Iverson
This article examines seven ways to iterate over JavaScript objects—excluding arrays, Map, and Set—using a sample player object modeled after Allen Iverson, detailing each method’s handling of own, inherited, enumerable, non‑enumerable, and Symbol properties, with code examples and output analysis.
Introduction
This article summarizes seven traversal methods for plain JavaScript objects (excluding arrays,
Map, and
Set). The examples use a
playerobject that mimics the basketball star Allen Iverson, allowing us to observe how each method treats own, inherited, enumerable, non‑enumerable, and
Symbolproperties.
Define the Object
<code>const player = {
name: 'Allen Iverson',
[Symbol('birthday')]: '1975/06/07',
};
Object.defineProperties(player, {
isHallofFame: { enumerable: false, value: true },
[Symbol('ScoreKingTime')]: { enumerable: false, value: 4 },
});
Object.defineProperties(player.__proto__, {
university: { enumerable: true, value: 'Georgetown' },
team: { enumerable: false, value: '76ers' },
[Symbol('country')]: { enumerable: true, value: 'USA' },
[Symbol('hometown')]: { enumerable: false, value: 'Virginia' },
});
</code>The object has eight properties, a mix of string‑keyed and
Symbolkeys, some enumerable and some not, plus prototype properties.
for...in
MDN: The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
<code>for (const name in player) {
console.log('name:', name);
}
// name: name
// name: university
</code>The loop enumerates own and prototype enumerable string‑keyed properties but skips
Symbolkeys. A common guard is
Object.prototype.hasOwnProperty.call(player, name)to exclude prototype properties.
Object.keys
MDN: Object.keys() returns an array of a given object's own enumerable property names , iterated in the same order as a normal loop would.
<code>const keys = Object.keys(player);
console.log('keys:', keys);
// keys: ["name"]
</code> Object.keysreturns only the object's own enumerable string‑keyed properties (no
Symbol, no prototype properties).
Object.getOwnPropertyNames
MDN: Object.getOwnPropertyNames() returns an array of all properties (including non‑enumerable properties except those using Symbol ) found directly in a given object.
<code>const ownPropertyNames = Object.getOwnPropertyNames(player);
console.log('ownPropertyNames:', ownPropertyNames);
// ownPropertyNames: ["name", "isHallofFame"]
</code>This method returns all own properties, enumerable or not, but excludes
Symbolkeys.
Object.getOwnPropertySymbols
MDN: Object.getOwnPropertySymbols() returns an array of all Symbol properties found directly upon a given object.
<code>const ownPropertySymbols = Object.getOwnPropertySymbols(player);
console.log('ownPropertySymbols:', ownPropertySymbols);
// ownPropertySymbols: [Symbol(birthday), Symbol(ScoreKingTime)]
</code>It returns every own
Symbolproperty, regardless of enumerability, and does not include prototype symbols.
Reflect.ownKeys
MDN: The static Reflect.ownKeys() method returns an array of the target object's own property keys.
<code>const ownKeys = Reflect.ownKeys(player);
console.log('ownKeys:', ownKeys);
// ownKeys: ["name", "isHallofFame", Symbol(birthday), Symbol(ScoreKingTime)]
</code> Reflect.ownKeysreturns all own property keys, both string‑keyed and
Symbol, enumerable or not.
Object.values
MDN: Object.values() returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (which also includes prototype properties).
<code>const values = Object.values(player);
console.log('values:', values);
// values: ["Allen Iverson"]
</code>Like
Object.keys, it only includes own enumerable string‑keyed properties, but returns the values instead of the keys.
Object.entries
MDN: The Object.entries() method returns an array of a given object's own enumerable string‑keyed [key, value] pairs, in the same order as a for...in loop (excluding prototype properties).
<code>const entries = Object.entries(player);
console.log('entries:', entries);
// entries: [["name", "Allen Iverson"]]
</code>It behaves like
Object.keysbut returns both keys and values, useful together with
Object.fromEntriesfor transformations.
Summary Table
for...in : includes prototype enumerable properties; excludes non‑enumerable and
Symbolkeys.
Object.keys : own enumerable string‑keyed properties only.
Object.getOwnPropertyNames : own properties, including non‑enumerable, but excludes
Symbolkeys.
Object.getOwnPropertySymbols : own
Symbolproperties (including non‑enumerable), no prototype symbols.
Reflect.ownKeys : all own property keys (string‑keyed and
Symbol, enumerable or not).
Object.values : own enumerable string‑keyed property values only.
Object.entries : own enumerable string‑keyed
[key, value]pairs.
Conclusions
Only
for...incan traverse prototype properties.
Object.getOwnPropertyNamesand
Reflect.ownKeyscan retrieve non‑enumerable properties.
Object.getOwnPropertySymbolsand
Reflect.ownKeyscan retrieve
Symbolproperties.
WeDoctor Frontend Technology
Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.