0

I have been using Leaflet as of late and enjoy the "lightness" of this API vs. OpenLayers. One of the things I am doing is looking at examples and/or source code from pages that use this API. I recently came across some code that I am at a complete loss as to the method it is using to call a method. There are several places where calls are made using _.[method name], but I have no idea what the underscore is referring to. Ex. _.sortBy(flights, function (value). Is this just creating a new function?? I'm used to the $ used for jQuery, but this underscore is new to me and I cannot find anything on the web about this. Can anyone help? Or am I barking up the wrong tree and this code has nothing to do with leaflet??

A code block is included for reference.

map = L.map('map').setView([44.715514, -112.148438], 4);

// Add the Stamen toner tiles as a base layer
var baseLayer = new L.StamenTileLayer('toner', {
    detectRetina: true
}).addTo(map);

// Add a layer control
var layerControl = L.control.layers().addTo(map);

// Add a legend control
var legendControl = L.control.legend({
    autoAdd: false
}).addTo(map);

// Create a lookup of airports by code.  NOTE:  this is easy, but non-optimal, particularly with a large dataset
// Ideally, the lookup would have already been created on the server or created and imported directly
var airportsLookup = L.GeometryUtils.arrayToMap(airports, 'code');

// Sort flight data in descending order by the number of flights.  This will ensure that thicker lines get displayed
// below thinner lines
flights = _.sortBy(flights, function (value) {
    return -1 * value.cnt;
});

// Group flight data by airline code
var airlineLookup = _.groupBy(flights, function (value) {
    return value.airline;
});

var maxCountAll = Number(flights[0].cnt);

// Get the top count of flights
flights = _.filter(flights, function (value) {
    return value.airline !== 'all';
});
2

Those are methods from either underscore or lodash being used in those samples. They are not part of, nor require for Leaflet. The naming convention is one where I suppose searching for "_" would not be very fruitful if you weren't familiar with it already.

  • Thanks so much for that answer. I do see a a script tag above that includes underscore.js. I looked it over and see why it is being used! Thanks again for the help! – ringraham Feb 17 '15 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.