This module provides a function to find an element from an object by the specified parameters.
The function is useful for declarative searching from data without functions, i.e., we can take parameters to search from JSON
or other stuff.
When the search function takes a plain object and some where condition, it returns the object itself if it matches the condition.
Otherwise, it returns undefined.
importselectfrom'core/object/select';
// {foo: 1, bar: 2} select({foo:1, bar:2}, {where: {foo:1}});
When the search function takes an iterable object and some where condition, it returns the first element from the object
that matches the condition. If there are no elements that match the condition, the function returns undefined.
importselectfrom'core/object/select';
// {foo: 1, bar: 2} select([{bla:12}, {foo:1, bar:2}], {where: {foo:1}});
core/object/select
This module provides a function to find an element from an object by the specified parameters. The function is useful for declarative searching from data without functions, i.e., we can take parameters to search from JSON or other stuff.
Searching upon a plain object
When the search function takes a plain object and some
where
condition, it returns the object itself if it matches the condition. Otherwise, it returnsundefined
.The matching process is recursive, i.e., it checks the nested structure of the condition and object.
If the object to search doesn't have some property from
where
, the property will be ignored if other conditions are matched.Searching upon an iterable object
When the search function takes an iterable object and some
where
condition, it returns the first element from the object that matches the condition. If there are no elements that match the condition, the function returns undefined.Providing multiple conditions to search
When
where
contains an array, it will be represented as theOR
condition.Providing a context to search
By using the
from
option, you can define a start point to search.If there is no provided
where
condition but specifiedfrom
, the function returns a value by the path.