當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


underscore.js findWhere()用法及代碼示例

Underscore.js是一個JavaScript庫,提供了許多有用的函數,即使在不使用任何內置對象的情況下,也可以極大地幫助您進行編程,例如映射,過濾,調用等。
_.findWhere()函數用於獲取與給定屬性匹配的所有元素的列表。 _.findWhere()函數用於搜索整個節列表中的內容。將顯示包含內容的部分。

用法:

_.findWhere(list, properties) 

參數:它有兩個參數:


  • List:此參數包含項目列表。
  • Property:此參數包含測試條件。

返回值:返回從列表中選擇的元素的詳細信息。僅匹配的第一個元素將作為輸出給出。

_.findWhere()和_.where()函數之間的區別:這兩個函數都使用數組名稱和要匹配的屬性,但是_.where()函數顯示所有匹配項,而_.findWhere)函數僅匹配第一個匹配項。

在數組中搜索屬性:._findWhere()函數一個接一個地獲取數組元素,並匹配給定的屬性是否相同。如果屬性匹配,則顯示該特定元素的其餘詳細信息。第一次匹配該屬性後,_。findWhere()函數結束。它僅顯示第一個匹配項。

例:

<html> 
    <head> 
        <title>_.contains() function</title> 
        <script type="text/javascript" src= 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
        </script> 
    </head>        
    <body> 
        <script type="text/javascript"> 
            var users = [{id:1, name:"harry"}, {id:2, name:"jerry"}]; 
            _.findWhere(users, {id:2}).name = 'tom'; 
            console.log(users); 
        </script> 
    </body>   
</html>

輸出:

將具有許多不同屬性的元素列表傳遞給_.findWhere()函數:首先,聲明具有所有元素及其特定屬性的數組。然後將數組名稱以及需要與_.findWhere()函數匹配的屬性一起傳遞。所有其餘的屬性將顯示為該特定元素的輸出。

例:

<html> 
    <head> 
        <title>_.contains() function</title> 
        <script type="text/javascript" src= 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
        </script> 
    </head>        
    <body> 
        <script type="text/javascript"> 
            var goal  = [ 
                { 
                    "category":"other", 
                    "title":"harry University", 
                    "value":50000, 
                    "id":"1" 
                }, 
                { 
                    "category":"traveling", 
                    "title":"tommy University", 
                    "value":50000, 
                    "id":"2" 
                }, 
                { 
                    "category":"education", 
                    "title":"jerry University", 
                    "value":50000, 
                    "id":"3" 
                }, 
                {     
                    "category":"business", 
                    "title":"Charlie University", 
                    "value":50000, 
                    "id":"4" 
                } 
            ] 
            console.log(_.findWhere(goal, {category:"education"})); 
        </script> 
    </body>   
</html>

輸出:

將具有“ true /false”作為屬性的數組傳遞給_.findWhere()函數:首先聲明該數組(此處的數組為“ people”)。該屬性(此處為“ hasLong”)定義為“ true”或“ false”。選擇一種條件進行檢查,例如此處的“ hasLongHairs”。 Console.log顯示最終答案。

例:

<html> 
    <head> 
        <title>_.contains() function</title> 
        <script type="text/javascript" src= 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
        </script> 
    </head>        
    <body> 
        <script type="text/javascript"> 
            var people = [ 
                {"name":"sakshi", "hasLong":"false"}, 
                {"name":"aishwarya", "hasLong":"true"}, 
                {"name":"akansha", "hasLong":"true"}, 
                {"name":"preeti", "hasLong":"true"} 
            ] 
         console.log(_.findWhere(people, {hasLong:"true"})); 
        </script> 
    </body> 
</html>

輸出:

將數字數組作為屬性一起傳遞給_.findWhere()函數:它也遵循相同的過程,首先聲明具有所有屬性的數組,並為_.findWhere()函數提供數組名稱和匹配的屬性。

例:

<html> 
    <head> 
        <title>_.contains() function</title> 
        <script type="text/javascript" src= 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
        </script> 
    </head>        
    <body> 
        <script type="text/javascript"> 
             var users = [{"num":"1"},  
                          {"num":"2"},  
                          {"num":"3"},  
                          {"num":"4"},  
                          {"num":"5"}]; 
        console.log(_.findWhere(users, {num:"2"})); 
        </script> 
    </body> 
    
</html>

輸出:



相關用法


注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js | findWhere() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。