Lodash.js – indexOf() function

A simple Lodash.js example of the indexOf() function, we will return the index of the element from its first occurence in the collection.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen example"/>
    <title>Tizen lodash.js example</title>
    <script src="js/lodash.min.js"></script>
</head>
<body>
<div id="first">
</div>
<br>
<div id="second">
</div>

<script>

    var firstDiv = document.getElementById('first');
    var secondDiv = document.getElementById('second');

    var firstValue = [1,4,7,23,41,63,12,34,41,41,23];

  firstDiv.innerHTML = "Having the array of values, we will find the index of the first occurence of the value 41 in the provided array...";

    var result = _.indexOf(firstValue, 41);

  secondDiv.innerHTML = "...and we will get the index of: " + result;

</script>
</body>

</html>

Responses

0 Replies