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


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


Underscore.js是一個JavaScript庫,提供了許多有用的函數,即使在不使用任何內置對象的情況下,也可以極大地幫助您進行編程,例如映射,過濾,調用等。
_.size()函數用於查找數組的大小,即數組中元素的數量。它主要用於查找數組中元素的數量。

用法:

_.size( list ) 

參數:此函數接受單個參數列表,該列表用於保存項目列表。


返回值:該函數返回一個值,該值是傳遞的數組的大小。

將數組傳遞給_.size()函數:._size()函數從列表中一個接一個地獲取元素,並從零開始計算大小,並在通過每個元素時遞增一。傳遞一個數組,該數組首先具有屬性名稱,然後將其元素作為數組的一個元素。如果未提及屬性名稱,則_.size()函數將不起作用。

例:

<!DOCTYPE html> 
<html> 
    <head> 
        <script src =  
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
        </script> 
    </head> 
    <body> 
        <script type="text/javascript"> 
            var list = [1, 2, 3, 4, 5]; 
            console.log(_.size(list)); 
        </script> 
    </body> 
</html>                    

輸出:

將大量屬性數組傳遞給_.size()函數:將大量數組傳遞給_.size()函數。 _.size()函數采用一個數組,該數組具有4個屬性,即“類別”,“標題”,“值”和“ id”。然後在此陣列和控製台上應用_.size()函數.log()將顯示最終結果。

例:

<!DOCTYPE html> 
<html> 
    <head> 
        <script 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(_.size(goal)); 
        </script> 
    </body> 
</html>                    

輸出:

將結構傳遞給_.size()函數:首先聲明一個數組(此處的數組為“ people”)。然後隻需將此數組傳遞給_.size()函數和console.log()將顯示最終答案。

例:

<!DOCTYPE html> 
<html> 
    <head> 
        <script 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(_.size(people)); 
        </script> 
    </body> 
</html>                    

輸出:

僅將一個屬性數組傳遞給_.size()函數:聲明一個包含一個屬性的數組。數組“用戶”是用一個屬性“ num”定義的。然後,隻需在_.size()函數中提及該數組的名稱,即可將該數組傳遞給_.size()函數。最後,console.log()將顯示最終答案。

例:

<!DOCTYPE html> 
<html> 
    <head> 
        <script 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(_.size(users)); 
        </script> 
    </body> 
</html>                    

輸出:



相關用法


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