JS: for-in Loop
for (key in obj) { body }
-
Loop thru property keys that are Enumerable Property and is a string key, including all such properties in the Prototype Chain.
Sets the property name to key in body.
π‘ TIP: don't use for-in loop, because you probably don't want to go thru parent chain. Use for-of Loop, or Object.keys and forEach .
const aa = { ak1: 1, ak2: 2 }; const bb = { bk1: 1, bk2: 2 }; // make aa to be parent of bb Reflect.setPrototypeOf(bb, aa); const xx = []; // loop thru enumerable properties in prototype chain or own for (let kk in bb) console.log(kk); /* prints bk1 bk2 ak1 ak2 */
JavaScript, Property
- JS: Property Overview
- JS: Property Key
- JS: Property Dot Notation vs Bracket Notation
- JS: Create Property
- JS: Delete Property
- JS: Get Set Property
- JS: Check Property Existence
- JS: Access Property
- JS: List Properties
- JS: for-in Loop
- JS: Enumerable Property
- JS: Property Attributes
- JS: Property Descriptor
- JS: Getter Setter Properties