Lodash.js – findKey() function

A simple Lodash.js example of the findKey() function, which finds keys inside collections.
<!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 = {
                       'Bart':  { 'age': 31, 'driverLicense': true, 'certified':true },
                       'Josh':    { 'age': 16, 'driverLicense': false, 'certified':true  },
                       'Rad': { 'age': 45,  'driverLicense': true, 'certified':false  }
                     };

    firstDiv.innerHTML = "Having an object with data objects:<br><br>";
    firstDiv.innerHTML += JSON.stringify(firstValue);

    var result = _.findKey(firstValue, function(person) {

        return person.age < 35;

    });

    secondDiv.innerHTML = "...we will find the first person whose age is lower than 35... and we get:<br><br>";
    secondDiv.innerHTML += JSON.stringify(result);

</script>
</body>

</html>

Responses

0 Replies