filter
Filter a list to a new list of the matched items.
list.filter( callback [,thisArg] )
Filters list based on the return value of callback.
import{DefineList} from "can";
const names = new DefineList(["alice","adam","zack","zeffer"]);
const aNames = names.filter((name) => {
return name[0] === "a";
});
console.log(aNames.get()); //-> ["alice","adam"]
Parameters
- callback
{function(item, index, list)}:A function to call with each element of the DefineList. The three parameters that callback gets passed are:
- item (*) - the element at index.
- index (Integer) - the index of the current element of the list.
- list (DefineList) - the
DefineListthe elements are coming from.
If
callbackreturns a truthy result,itemwill be added to the result. Otherwise, theitemwill be excluded. - thisArg
{Object}:What
thisshould be in thecallback.
Returns
A new instance of this DefineList (may be a subclass), containing the items that passed the filter.
list.filter( props )
Filters items in list based on the property values in props.
import { DefineList } from "can";
const todos = new DefineList([
{name: "dishes", complete: false},
{name: "lawn", complete: true}
]);
const complete = todos.filter({complete: true});
console.log(complete.get()); //-> [{name: "lawn", complete: true}]
Parameters
- props
{Object}:An object of key-value properties. Each key and value in
propsmust be present on anitemfor theitemto be in the returned list.