Current State of JavaScript Functional Programming Libraries [closed]
Current State of JavaScript Functional Programming Libraries [closed]
Current State of JavaScript Functional Programming Libraries [closed]
You can use map and omit in conjunction to exclude specific properties, like this: var newArr = _.map(arr, function(o) { return _.omit(o, ‘c’); }); Or map and pick to only include specific properties, like this: var newArr = _.map(arr, function(o) { return _.pick(o, ‘q’); });
If the array contains either nulls or objects then you could use compact: var everythingButTheNulls = _.compact(list); NB compact removes all falsy values so if the array could contain zeros, false etc then they would also be removed. Could also use reject with the isNull predicate: var everythingButTheNulls = _.reject(array, _.isNull);
One can use apply to pass an arbitrary number of arguments to a method. For union: // Outputs [1, 4, 5, 6, 2, 3, 7] var selectedUnion = _.union.apply(_, selected); For intersection: // Outputs [1] var selectedIntersection = _.intersection.apply(_, selected);
Use _.find instead of findWhere: console.log(_.find(response.data, function(item) { return item.TaskCategory.TaskCategoryId == $routeParams.TaskCategory; })); They are similar, but findWhere is designed for special cases where you want to match key-value pairs (not useful in your scenario as it involves nested objects). Find is more general-use, because it lets you provide a function as the predicate.
Here are two vanilla javascript options: A.: Iterate over the object’s keys and delete those having a falsey value. var obj = { propA: true, propB: true, propC: false, propD: true, }; Object.keys(obj).forEach(key => { if (!obj[key]) delete obj[key]; }); console.log(obj); See Object.keys() and Array.prototype.forEach() B.: Iterate over the object’s keys and add truthy values … Read more
Go to “Browse Packages” in the menu (where the menu item is depends on your platform). Open up HTML/HTML.tmLanguage Change this line (line 286 in my HTML.tmLanguage): <string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)</string> to this: <string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)(?!.*type=[“‘]text/template[‘”])</string> Now any script tags with type=”text/template” or type=”text/template” will render as html and not javascript.
With Lodash (fork of underscore) you can. Lodash’s _.extend method accept third (or higher) parameter to be a function, that receives values (old and new); So you can do something like this: var deep = function(a, b) { return _.isObject(a) && _.isObject(b) ? _.extend(a, b, deep) : b; }; var a = {a:{b:{c:1}}}, b = … Read more
Object.keys gets the keys from the object, then you can filter the keys based on the values var obj = {1001: true, 1002: false}; var keys = Object.keys(obj); var filtered = keys.filter(function(key) { return obj[key] }); FIDDLE
Use groupBy with a function that creates a composite key using user_id and alert_id. Then map across the groupings to get what you want: var list = [ { user_id: 301, alert_id: 199, deal_id: 32243 }, { user_id: 301, alert_id: 200, deal_id: 32243 }, { user_id: 301, alert_id: 200, deal_id: 107293 }, { user_id: 301, … Read more