Object.filter
Object.filter
Function
Filters the key-value pairs for which the callback result is true and returns a new object.
Syntax
object.filter(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 new object after filtering. |
Return object fields
The returned object only contains the fields in the original object that make the callback result true. The field name remains unchanged, and the field value remains the original value; the field that fails the filter does not exist, and the reading result is empty.
Example
data = {a: 1, b: 2, c: 3} result = data.filter(fn(value) { value > 1 }) // Output: {"b":2,"c":3} print result
Notes
- filter does not modify the original object.
- The callback traverses the object snapshot at the beginning of the call.