Grammar:
_.filter (list, predicate, [context])
Description
Matches each member of the list collection sequentially (based on the predicate iteration function detection), returning a collection of successful matches
- List can be arrays, objects, strings, and arguments
- Predicate will pass the third parameter value, key, list (parameter name can be customized)
- predicate function needs return value
- Context can change the inside of the predicate function this
Code example: Example one: Filter array, object, string, arguments, and return data matching success
varresult;//manipulating Arraysresult = _.filter ([1, 2, 3],function(value) {returnValue% 2 = = = 1;}); Console.log (Result)//= = [1, 3]//manipulating Objectsresult = _.filter ({one: ' A ', two: ' II ', three: ' Three ', num:123, BOOL:false},function(value) {return typeofValue = = = "string";}); Console.log (Result)//= = ["One", "two", "three"]//manipulating Complex objectsvarobj ={levela: {level0:' Level0 ', Level1:' Level1 '}, Levelb:A, LEVELC:1, Leveld: {level3:' Level3 '}}result= _.filter (obj,function(value) {return typeofValue = = = "Object";}); Console.log (Result)//= = [{level0: "Level0", Level1: "Level1", {level3: ' Level3 '}}]//Action String (here the character is split into an array)result = _.filter (' 123 ',function(value) {returnValue = = = ' 2 ' | | Value = = = ' 3 ';}); Console.log (Result)//= = ["2", "3"]//Operation ArgumentsfunctionABC () {Result= _.filter (arguments,function(value) {returnValue% 2 = = = 1; }); Console.log (result); //= = [1, 3]}ABC (1, 2, 3);
Example two: Parameters passed by the predicate function (return return value is required inside the function, otherwise [])
varresult;//the case of an arrayresult = _.filter ([1, 2, 3],function(value, key, list) {Console.log (value, key, list); //= 1 0 [1, 2, 3] //= 2 1 [1, 2, 3] //= 3 2 [1, 2, 3]}); Console.log (Result)//= []//the case of the objectresult = _.filter ({one: ' A ', two: ' II ', three: ' Three '},function(value, key, list) {Console.log (value, key, list); //= = One Object {one: "A", Two: "II", Three: "Three"} //= Two two Object {one: "I", "II", Three: "Three"} //= Three three Object {one: "A", Two: "II", Three: "Three"}}); Console.log (Result)//= []
Example three: Context can change the internal predicate this
varresult;//the case of an arrayresult = _.filter ([1, 2, 3],function(value, key, list) {Console.log ( This);//= = [1, 2, 3] This is an array}, [1, 2, 3]);//the case of the objectresult = _.filter ([1, 2, 3],function(value, key, list) {Console.log ( This);//= = Object {No:10} This is a}, {"No": 10 });//case of a stringresult = _.filter ([1, 2, 3],function(value, key, list) {Console.log ( This);//= = String {0: "1", 1: "2", 2: "3", Length:3, [[Primitivevalue]]: "123"} //This is the object after the string has been split}, "123");
The _.select function is the same as the _.filter.
var function (value, key, list) { return value% 2 = == 1//= =[1, 3]
Special case Example One: special case of List
// For example: Null,undefined,0,true,this etc; var result = _.filter (nullfunction (value, key, list) { return< /c15>true//= = []
Example two: The case of the predicate function that is the window global object
// Example: null,undefined,window,this, etc. var function (value, key, list) { Console.log (this//= = This is the window global object null) ;
Can the list parameter be a true or false value?
var true NULL //= [1, True, "1"]
predicate have other written wording? Example one: When the predicate parameter is empty
var result = _.filter ({x:1, y:2//= = [1, 2]
Example two: When the predicate parameter is a single character
var result = _.filter ([{x:1}, {y:2}], ' x '//= = [{x:1}]
Example three: When the predicate parameter is an object
var obj = [ 1, Y:2}, 1}, 2, Z:3}]var result = _ . Filter (obj, {x:1//= = [{x:1, y:2},{x:1}]
Underscorejs _.filter (list, predicate, [context])