name: duk_enum proto: | void duk_enum(duk_context *ctx, duk_idx_t obj_idx, duk_uint_t enum_flags); stack: | [ ... obj! ... ] -> [ ... obj! ... enum! ] summary: |

Create an enumerator for object at obj_idx. Enumeration details can be controlled with enum_flags. If the target value is not an object, throws an error.

Enumeration flags:

DUK_ENUM_INCLUDE_NONENUMERABLE Enumerate also non-enumerable properties, by default only enumerable properties are enumerated
DUK_ENUM_INCLUDE_HIDDEN Enumerate also hidden Symbols, by default hidden Symbols are not enumerated. Use together with DUK_ENUM_INCLUDE_SYMBOLS. In Duktape 1.x this flag was called DUK_ENUM_INCLUDE_INTERNAL.
DUK_ENUM_INCLUDE_SYMBOLS Include Symbols in the enumeration result. Hidden Symbols are not included unless DUK_ENUM_INCLUDE_HIDDEN is specified.
DUK_ENUM_EXCLUDE_STRINGS Exclude strings from the enumeration result. By default strings are included.
DUK_ENUM_OWN_PROPERTIES_ONLY Enumerate only an object's "own" properties, by default also inherited properties are enumerated
DUK_ENUM_ARRAY_INDICES_ONLY Enumerate only array indices, i.e. property names of the form "0", "1", "2", etc
DUK_ENUM_SORT_ARRAY_INDICES Apply the ES2015 [[OwnPropertyKeys]] enumeration order over the whole enumeration result rather than per inheritance level, this has the effect of sorting array indices (even when inherited)
DUK_ENUM_NO_PROXY_BEHAVIOR Enumerate a Proxy object itself without invoking Proxy behaviors.

Without any flags the enumeration behaves like for-in: own and inherited enumerable properties are included, and enumeration order follows the Ecmascript ES2015 [[OwnPropertyKeys]] enumeration order, applied for each inheritance level.

Once the enumerator has been created, use duk_next() to extract keys (or key/value pairs) from the enumerator.

The ES2015 [[OwnPropertyKeys]] enumeration order is: (1) array indices in ascending order, (2) non-array-index keys in their insertion order, and (3) symbols in their insertion order. This rule is applied separately for each inheritance level, so that if array index keys are inherited, they will appear out-of-order in the result. For most practical code this is not an issue because array indices are very rarely inherited. You can force the whole enumeration sequence to be sorted using DUK_ENUM_SORT_ARRAY_INDICES.
example: | duk_enum(ctx, -3, DUK_ENUM_INCLUDE_NONENUMERABLE); while (duk_next(ctx, -1 /*enum_idx*/, 0 /*get_value*/)) { /* [ ... enum key ] */ printf("-> key %s\n", duk_get_string(ctx, -1)); duk_pop(ctx); /* pop_key */ } duk_pop(ctx); /* pop enum object */ tags: - object - property seealso: - duk_next introduced: 1.0.0