Underscorejs _.find (list, predicate, [context])

Source: Internet
Author: User

Grammar:

_.find (list, predicate, [context])

Description

Matches each member of the list collection sequentially (based on the predicate iteration function detection), and returns the current member immediately if the match succeeds

    • 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: Find Array, object, string, arguments, and return matching successful data
varresult;//manipulating Arraysresult = _.find ([1, 2, 3],function(value) {returnValue = = 2;}); Console.log (Result)//= 2        //manipulating Objectsresult = _.find ({one: ' A ', two: ' II ', three: ' Three '},function(value) {returnValue = = = ' two ';}); Console.log (Result)//= "Two"//manipulating Complex objectsvarobj ={levela: {level0:' Level0 ', Level1:' Level1 '}, Levelb:A, LEVELC:1}result= _.find (obj,function(value) {returnValue.level0 = = = ' Level0 ';}); Console.log (Result)//= = Object {level0: "Level0", Level1: "Level1"}//Action String (here the character is split into an array)result = _.find (' 123 ',function(value) {returnValue = = = ' 2 ';}); Console.log (Result)//= "2"//Operation ArgumentsfunctionABC () {Result= _.find (arguments,function(value) {returnValue = = 2;    }); Console.log (result); //= 2}ABC (1, 2, 3);
Example two: Parameters passed by the predicate function (return return value is required inside the function, otherwise undefined)
varresult;//the case of an arrayresult = _.find ([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]    return true;//true to return the first member directly});//the case of the objectresult = _.find ({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"}    return true;//true to return the first member directly});
Example three: Context can change the internal predicate this
varresult;//the case of an arrayresult = _.find ([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 = _.find ([1, 2, 3],function(value, key, list) {Console.log ( This);//= = Object {No:10} This is a}, {"No": 10 });//case of a stringresult = _.find ([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 _.detect function is the same as the _.find.
var function (value, key, list) {    return value = == 2//=2
Special case Example One: special case of List
// For example: Null,undefined,0,true,this etc; var result = _.find (nullfunction  (value, key, list) {    returnTrue //= "undefined"  
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  result = _.find ([falsetruenull//= = [1]
predicate have other written wording? Example one: When the predicate parameter is empty
var  result = _.find ({x:1, y:2//== [1]
Example two: When the predicate parameter is a single character
var  result = _.find ([{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 = _ . Find (obj, {x:1//= = [{x:1, y:2}]

Underscorejs _.find (list, predicate, [context])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.