break array of objects into separate arrays based on a property

This is an easy job for Array.reduce(…): function groupBy(arr, property) { return arr.reduce(function(memo, x) { if (!memo[x[property]]) { memo[x[property]] = []; } memo[x[property]].push(x); return memo; }, {}); } var o = groupBy(arr, ‘type’); // => {orange:[…], banana:[…]} o.orange; // => [{“type”:”orange”,”title”:”First”},{“type”:”orange”,”title”:”Second”}] o.banana; // => [{“type”:”banana”,”title”:”Third”},{“type”:”banana”,”title”:”Fourth”}] Of course, if your target browser(s) do not support ECMAScript … Read more

How do you clone an array of objects using underscore?

Well, there is a trick! If clone does not “clone” nested objects, you can force it to by explicitly cloning each object inside a map call! Like this: #!/usr/bin/env node var _ = require(‘underscore’); var a = [{f: 1}, {f:5}, {f:10}]; var b = _.map(a, _.clone); // <—- b[1].f = 55; console.log(JSON.stringify(a)); Prints: [{“f”:1},{“f”:5},{“f”:10}] Yay! … Read more

need explanation of the _.bindAll() function from Underscore.js

var Cow = function(name) { this.name = name; } Cow.prototype.moo = function() { document.getElementById(‘output’).innerHTML += this.name + ‘ moos’ + ‘<br>’; } var cow1 = new Cow(‘alice’); var cow2 = new Cow(‘bob’); cow1.moo(); // alice moos cow2.moo(); // bob moos var func = cow1.moo; func(); // not what you expect since the function is called … Read more

ESLint’s “no-undef” rule is calling my use of Underscore an undefined variable

The official documentation should give you an idea on how to fix this. Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global …*/ comment, or specified in the globals key in the configuration file. The easiest fix would be to add /* global _ */ at … Read more

Remove empty properties / falsy values from Object with Underscore.js

Since Underscore version 1.7.0, you can use _.pick: _.pick(sourceObj, _.identity) Explanation The second parameter to _.pick can be a predicate function for selecting values. Values for which the predicate returns truthy are picked, and values for which the predicate returns falsy are ignored. pick _.pick(object, *keys) Return a copy of the object, filtered to only … Read more

Debounce function with args underscore

You don’t need an anonymous function in the middle, arguments will automatically be passed to the original function when you run the debounced version. var debounceCalculate = _.debounce(calculateLayout, 300); debounceCalculate(a,b); As an advantage you don’t have to hardcode-bind the arguments in advance You can try it and if curious just check the source

Missing Javascript “.map” file for Underscore.js when loading ASP.NET web page [duplicate]

What you’re experiencing is source mapping. This allows you to debug with readable code in your browser’s developer tools when working with minified JS files. The minified version of Underscore has this line at the end of the file: //# sourceMappingURL=underscore-min.map Your browser’s developers tools will try to download underscore-min.map when encountering this line. If … Read more

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