Lo-Dash

In this article:


    Lo-Dash is a Javascript tool or library that we have made available with the app.js file. This means that you, like our developers, have access to use it on the templates you develop.

    Lo-Dash, that can be accessed via the variable _ (underscore) provides powerful tools for manipulating objects, arrays and collections, but also a micro templating language that can be used to build elements on a website via Javascript as well. In addition to the examples here, there is also a lot of documentation about Lo-Dash on their website and in their documentation at https://lodash.com.

    See some brief examples below of how Lo-Dash can be used:

     

    _.pluck function:

    
    (function () {
        var characters = [
          { 'name': 'barney', 'age': 36 },
          { 'name': 'fred',   'age': 40 }
        ];
    
        _.pluck(characters, 'name');
        // → ['barney', 'fred']
    })();
    
    

    _.map function:

    
    (function () {
        _.map([1, 2, 3], function(num) { return num * 3; });
        // → [3, 6, 9]
    
        _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
        // → [3, 6, 9] (property order is not guaranteed across environments)
    
        var characters = [
          { 'name': 'barney', 'age': 36 },
          { 'name': 'fred',   'age': 40 }
        ];
    
        // using "_.pluck" callback shorthand
        _.map(characters, 'name');
        // → ['barney', 'fred']
    })();
    
    

    _.template function:

    
    (function () {
        var compiled = _.template('hello <%= name %>');
        compiled({ 'name': 'fred' });
        // → 'hello fred'
    
        _.template('hello <%= name %>', { 'name': 'world' });
        // → 'hello world!'
    })();
    
    

    _.random function:

    
    (function () {
        _.random(0, 100);
        // → tilfældigt heltal mellem 0 and 100
    })();
    
    

    _.range function:

    
    (function () {
        _.range(4);
        // → [0, 1, 2, 3]
    })();