lodash 数组元素查找 findIndex

_.findIndex(array, [predicate=_.identity])

这个方法类似 _.find。除了它返回最先通过 predicate 判断为真值的元素的 index ,而不是元素本身。

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>数组元素查找</title>
    </head>

    <body>
        <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
        <script type="text/javascript">
            var users = [{
                    'user': 'barney',
                    'active': false
                },
                {
                    'user': 'fred',
                    'active': false
                },
                {
                    'user': 'pebbles',
                    'active': true
                }
            ];
            var index = _.findIndex(users, {
                'user': 'fred',
                'active': false
            });
            console.log(index);
            //[3]
        </script>
    </body>

</html>
原文地址:https://www.cnblogs.com/mengfangui/p/9209841.html