List.filter method

0 out of 3 rated this helpful - Rate this topic

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.


<div id="result"></div>
<script type="text/javascript">
    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);

    document.getElementById("result").textContent = filterArr.join();

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

// Output:
// abcd,abcdef

Requirements

Namespace

WinJS.Binding

Library

Base.js

See also

List

 

 

Build date: 12/5/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.