다음을 통해 공유


List.filter method

Returns the elements of a list that meet the condition specified in a callback function.

Syntax

var array = list.filter(callback, thisArg);

Parameters

  • callback
    Type: Function

    A function that accepts up to three arguments. The function is called for each element in the list.

    This function must always return the same results, given the same inputs. The results should not depend on values that are subject to change. You must call notifyMutated each time an item changes. Do not batch change notifications.

  • thisArg
    Type: Object

    An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used.

Return value

Type: Array

An array containing the elements that meet the condition specified in the callback function.

Examples

The following code shows how to use this method. The filter function selects only strings that have even-numbered lengths.

    var stringArr = [];
    stringArr.push("abc");
    stringArr.push("abcd");
    stringArr.push("abcde");
    stringArr.push("abcdef");

    var stringList = new WinJS.Binding.List(stringArr);

    var filterArr = stringList.filter(filterStrings);

    console.log(filterArr.join());

    function filterStrings(str) {
        if (str.length % 2 == 0)
            return true;
        else
             return false;
     }


// Output:
// abcd,abcdef

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding

See also

List