Split JavaScript array in chunks using Lodash

Take a look at lodash’ chunk: https://lodash.com/docs#chunk const data = [“a1”, “a2”, “a3”, “a4”, “a5”, “a6”, “a7”, “a8”, “a9”, “a10”, “a11”, “a12”, “a13”]; const chunks = _.chunk(data, 3); console.log(chunks); // [ // [“a1”, “a2”, “a3”], // [“a4”, “a5”, “a6”], // [“a7”, “a8”, “a9”], // [“a10”, “a11”, “a12”], // [“a13″] // ] <script src=”https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js”></script>

Find by key deep in a nested array

Recursion is your friend. I updated the function to account for property arrays: function getObject(theObject) { var result = null; if(theObject instanceof Array) { for(var i = 0; i < theObject.length; i++) { result = getObject(theObject[i]); if (result) { break; } } } else { for(var prop in theObject) { console.log(prop + ‘: ‘ + … Read more

External template in Underscore

Here is a simple solution: var rendered_html = render(‘mytemplate’, {}); function render(tmpl_name, tmpl_data) { if ( !render.tmpl_cache ) { render.tmpl_cache = {}; } if ( ! render.tmpl_cache[tmpl_name] ) { var tmpl_dir=”/static/templates”; var tmpl_url = tmpl_dir + “https://stackoverflow.com/” + tmpl_name + ‘.html’; var tmpl_string; $.ajax({ url: tmpl_url, method: ‘GET’, dataType: ‘html’, //** Must add async: false, … Read more

Underscore: sortBy() based on multiple attributes

sortBy says that it is a stable sort algorithm so you should be able to sort by your second property first, then sort again by your first property, like this: var sortedArray = _(patients).chain().sortBy(function(patient) { return patient[0].name; }).sortBy(function(patient) { return patient[0].roomNumber; }).value(); When the second sortBy finds that John and Lisa have the same room … Read more

Removing duplicate objects with Underscore for Javascript

.uniq/.unique accepts a callback var list = [{a:1,b:5},{a:1,c:5},{a:2},{a:3},{a:4},{a:3},{a:2}]; var uniqueList = _.uniq(list, function(item, key, a) { return item.a; }); // uniqueList = [Object {a=1, b=5}, Object {a=2}, Object {a=3}, Object {a=4}] Notes: Callback return value used for comparison First comparison object with unique return value used as unique underscorejs.org demonstrates no callback usage lodash.com shows … Read more

Use underscore inside Angular controllers

When you include Underscore, it attaches itself to the window object, and so is available globally. So you can use it from Angular code as-is. You can also wrap it up in a service or a factory, if you’d like it to be injected: var underscore = angular.module(‘underscore’, []); underscore.factory(‘_’, [‘$window’, function($window) { return $window._; … Read more

using lodash .groupBy. how to add your own keys for grouped output?

You can do it like this in both Underscore and Lodash (3.x and 4.x). var data = [{ “name”: “jim”, “color”: “blue”, “age”: “22” }, { “name”: “Sam”, “color”: “blue”, “age”: “33” }, { “name”: “eddie”, “color”: “green”, “age”: “77” }]; console.log( _.chain(data) // Group the elements of Array based on `color` property .groupBy(“color”) // … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)