inArray()

功能

在数组中查找指定值并返回它的索引(如果没有找到,则返回-1)。

语法

langx.inArray(item, array)

参数

  • item
    要查找的值
  • array
    一个数组,通过它来查找

范例

Example: 返回数组[4, “Pete”, 8, “John”]中指定元素的索引值。

<!DOCTYPE html>
<html>

<head>
    <style>
  div { color:blue; }
  span { color:red; }
    </style>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script>
    <script>
    require.config({
        baseUrl: "./",
        packages: [{
            name: "skylark",
            location: "../../../src/skylark"
        }, ]
    });
    </script>
</head>

<body>
    <div>"John" found at <span></span></div>
    <div>4 found at <span></span></div>
    <div>"Karl" not found, so <span></span></div>
    <div>"Pete" is in the array, but not at or after index 2, so <span></span></div>
    <script>
    require(["skylark/query", "skylark/langx"], function(query, langx) {
        var arr = [4, "Pete", 8, "John"];
        var spans = query("span");
        query(spans.eq(0)).text(langx.inArray("John", arr));
        query(spans.eq(1)).text(langx.inArray(4, arr));
        query(spans.eq(2)).text(langx.inArray("Karl", arr));
        query(spans.eq(3)).text(langx.inArray("Pete", arr, 2));
    });
    </script>
</body>

</html>