Underscore.js _.contains函数

Underscore.js _.contains函数

Underscore.js是一个JavaScript库,它提供了很多有用的功能,对编程有很大的帮助,如地图、过滤器、调用等,甚至不使用任何内置对象。
_.contains()函数用于检查某项是否在列表中给出。这个函数需要将列表传递给这个函数。如果列表中包含大量的项目,那么只需在前面定义列表并将列表的名称作为参数传递给_.contains()函数。

语法:

_.contains( list, value, [fromIndex] )

参数:该函数接受上面提到的和下面描述的三个参数。

  • list。该参数包含项目列表。
  • value。该参数用于存储需要在列表中搜索的值。
  • fromIndex。它是一个可选参数,用于保存开始搜索的索引。

返回值:该函数返回的值是true(当元素存在于列表中)或false(当元素不在列表中)。

  • 将一个数组传递给_.contains函数()。_.contains()函数从列表中逐个获取元素,并在列表中搜索给定的元素。在遍历列表中找到所需元素后,contains()函数结束,答案是true,否则答案是false。

示例:

<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>
        <script type="text/javascript" src=
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
        </script>
    </head>     
    <body>
        <script type="text/javascript">
              console.log(_.contains([1, 2, 3, 4, 5], 40) );
        </script>
    </body>
</html>

输出:

Underscore.js _.contains函数

示例:

<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>
        <script type="text/javascript" src=
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
        </script>
    </head>     
    <body>
        <script type="text/javascript">
              console.log(_.contains([1, 2, 3, 4, 5], 5) );
        </script>
    </body>
</html>

输出:

Underscore.js _.contains函数

  • 将一个字符串列表传递给_.contains()函数。将字符串列表传递给_.contains()函数,检查给定的字符串是否在列表中找到。如果字符串存在于列表中,那么它返回true,否则返回false。

示例:

<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>
        <script type="text/javascript" src=
        "https://cdnjs.cloudflare.com/ajax/libs/
         underscore.js/1.9.1/underscore.js">
        </script>
    </head>     
    <body>
        <script type="text/javascript">
             console.log(_.contains(['steven',
            'mack', 'jack'], 'steven'));
        </script>
    </body>
</html>

输出:

Underscore.js _.contains函数

  • 传递一个数组的数组到_.contains()函数。创建一个数组,并将数组名称传递给函数以指定元素。

示例:

<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>
        <script type="text/javascript" src=
        "https://cdnjs.cloudflare.com/ajax/libs
         /underscore.js/1.9.1/underscore.js">
        </script>
    </head>      
    <body>
        <script type="text/javascript">
             var indexes = [
            {'id': 1, 'name': 'simmy' },
            {'id':3, 'name': 'sam'}, 
            {'id': 5, 'name': 'sandeep'},
            {'id': 1, 'name': 'sahil' },
            {'id':3, 'name': 'shagun'}
        ];
        console.log(_.contains(indexes, {'id':1, 'name': 'jake'}));
        </script>
    </body>
</html>

输出:

Underscore.js _.contains函数

  • 将一个对象和一个数组传递给_.contains()函数。首先,定义一个对象变量并将其分配给{test: "test"}。然后创建一个包含其他元素如数字的数组,并将此对象作为一个数组元素加入。由于该对象被添加到数组中,因此答案将是true。
    示例:
<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>
        <script type="text/javascript" src=
        "https://cdnjs.cloudflare.com/ajax/libs
        /underscore.js/1.9.1/underscore.js">
        </script>
    </head>      
    <html>
    <body>
        <script type="text/javascript">
             var testObject = {test:"test"};
        var testArray = [1, 2, 3, testObject];
        console.log(_.contains(testArray, testObject));
        </script>
    </body>
</html>

输出:

Underscore.js _.contains函数

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程