Object.each
Object.each
Function
Iterate through object key values and execute callbacks.
Syntax
object.each(callback)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| callback | Fn | No | Empty | The callback signature is fn(value, key, current). |
Return value
| Type | Description |
|---|---|
| Object | Returns the original object to facilitate continued chain calls. |
Return object fields
each() returns the same object reference that it was called on. Field names and values retain the actual state of the original object at the end of the call; fields added, modified, or deleted during the callback will be reflected in the returned object.
Example
user = {name: 'BT', lang: 'Rust'} keys = [] user.each(fn(value, key) { keys.push(key) }) // Output: ["name","lang"] echo(keys)
Notes
- A shallow copy of the current content of the object is used during traversal. Callback modifications to the object will not affect the traversed items that have been taken out in this round.