1. 程式人生 > >underscorejs之_.find(list, predicate, [context])

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

contex 返回值 his eva undefine 語法 detect eve 否則

語法:

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

說明:

對list集合的每個成員依次進行匹配(根據predicate叠代函數檢測),匹配成功則立即返回當前成員

  • list可以為數組,對象,字符串和arguments
  • predicate會傳第三個參數value, key, list(參數名可自定義)
  • predicate函數需要返回值
  • context可以改變predicate函數內部的this

代碼示例:

示例一:find對數組,對象,字符串,arguments進行操作並返回匹配成功的數據

var result;

// 操作數組
result = _.find([1, 2, 3], function
(value) { return value === 2; }); console.log(result) //=> 2 // 操作對象 result = _.find({ one: ‘一‘, two: ‘二‘, three: ‘三‘ }, function (value) { return value === ‘二‘; }); console.log(result) //=> "二" // 操作復雜的對象 var obj = { levelA: { level0: ‘level0‘, level1: ‘level1‘ }, levelB:
‘一‘, levelC: 1 } result = _.find(obj, function (value) { return value.level0 === ‘level0‘; }); console.log(result) //=> Object {level0: "level0", level1: "level1"} // 操作字符串(此處將字符拆分為數組) result = _.find(‘123‘, function (value) { return value === ‘2‘; }); console.log(result) //=> "2" //操作arguments
function abc() { result = _.find(arguments, function (value) { return value === 2; }); console.log(result); //=> 2 } abc(1, 2, 3);

示例二:predicate函數傳遞的參數(函數內部需要return返回值,否則返回undefined)

var result;

//數組的情況
result = _.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為真值則直接返回第一個成員
});

//對象的情況
result = _.find({one: ‘一‘, two: ‘二‘, three: ‘三‘}, function(value, key, list){
    console.log(value, key, list);
    //=> 一 one Object {one: "一", two: "二", three: "三"}
    //=> 二 two Object {one: "一", two: "二", three: "三"}
    //=> 三 three Object {one: "一", two: "二", three: "三"}
    return true; // true為真值則直接返回第一個成員
});

示例三:context可以改變predicate內部的this

var result;

// 數組的情況
result = _.find([1, 2, 3], function (value, key, list) {
    console.log(this); //=> [1, 2, 3] this是數組
}, [1, 2, 3]);

// 對象的情況
result = _.find([1, 2, 3], function (value, key, list) {
    console.log(this); //=> Object {no: 10} this是對象
}, { "no": 10 });

// 字符串的情況
result = _.find([1, 2, 3], function (value, key, list) {
    console.log(this); //=> String {0: "1", 1: "2", 2: "3", length: 3, [[PrimitiveValue]]: "123"} this是將字符串拆分後的對象
}, "123");

_.detect的功能和_.find是一樣的

var result = _.detect([1, 2, 3], function (value, key, list) {
    return value === 2;
});
console.log(result) //=> 2

特殊情況

示例一:list的特殊情況

//例如:null,undefined,0,true,this等;
var result = _.find(null, function (value, key, list) {
    return true;
});
console.log(result) //=> "undefined"

示例二:predicate函數的this為window全局對象的情況

// 例如:null,undefined,window,this等
var result = _.find([1, 2, 3], function (value, key, list) {
    console.log(this); //=> this是window全局對象
}, null);

list參數可為真假值?

var  result = _.find([false, 1, true, ‘1‘, 0, undefined, null]);
console.log(result) //=> [1]

predicate還有其他寫法?

示例一:predicate參數為空的時候

var  result = _.find({x: 1, y: 2});
console.log(result) //=> [1]

示例二:predicate參數為一個字符的時候

var  result = _.find([{x: 1}, {y: 2}], ‘x‘);
console.log(result) //=> [{x: 1}]

示例三:predicate參數為對象的時候

var obj = [
    {x: 1, y: 2},
    {x: 1},
    {y: 2, z: 3}
]
var  result = _.find(obj, {x: 1});
console.log(result) //=> [{x: 1, y: 2}]

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