underscore.js

underscore where with or condition (underscore, lodash or any other solution)

让人想犯罪 __ 提交于 2019-12-21 19:25:19
问题 I implemented a mixin to add "or" condition with _.where var arr = [{a:1,b:4}, {a:5}, {a:6}, {a:11}]; _.mixin({ or: function(obj,arr,condition){ return _.chain(arr).where(condition).union(obj).value(); } }); now i can use it like this and it works perfectly somewhat like a sql query _.chain(arr).where({a:1}).or(arr,{a:11,b:3}).or(arr,{a:2}).value(); //returns [{a:1,b:4}] _.chain(arr).where({a:1}).or(arr,{a:11}).or(arr,{a:2}).value(); //returns [{a:1,b:4},{a:11}] _.chain(arr).where({a:1}).or

How do I find the intersection of an array of arrays that contain objects using Javascript/underscorejs?

孤者浪人 提交于 2019-12-21 12:29:15
问题 I can't figure out how to find the intersection of this set of arrays: [ [ {"name":"product1","light":"1"}, {"name":"product2","light":"2"}, {"name":"product5","light":"5"}, {"name":"product4","light":"4"} ], [ {"name":"product2","light":"2"}, {"name":"product3","light":"3"}, {"name":"product4","light":"4"} ],[...more arrays with objects] ] This is just sample data , the real set I have changes a lot but with that structure. I want the returned intersection to look like this (a single array

Using Backbone.js & Underscore, how to get a count of items from the model?

我只是一个虾纸丫 提交于 2019-12-21 05:36:13
问题 I have a notifications model for notifications // MODEL NotificationModel = App.BB.Model.extend({ defaults : {} }); // COLLECTION NotificationCollection = App.BB.Collection.extend({ model: NotificationModel, url: '/notifications', initialize : function() { var me = this; me.fetch(); } }); The collection is fetching from the server correctly and has the following fields (id, read) where read is true or false. How can I get the total number count of items that are read == false? ... Unread item

OSX inertia scrolling causing mousewheel.js to register multiple mousewheel events with the slightest scroll motion

泄露秘密 提交于 2019-12-21 05:21:27
问题 I have a pretty standard Flexslider instance I'm working on. Flexslider incorporates well with the jquery.mousewheel.js plugin, allowing slides to move on mousewheel events. However, Mac OSX and others employ 'intertia' to scrolling effects, making it virtually impossible to scroll only one slide left or right. The general effect is that even the slightest scroll motion in these situations triggers multiple mousewheel events and scrolls the slider multiple images left or right. Obviously,

How underscore memoize is implemented in javascript

倾然丶 夕夏残阳落幕 提交于 2019-12-21 05:14:11
问题 I'm developing my own functional-programming library, and now referring the underscore . memoize _.memoize(function, [hashFunction]) Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key. var fibonacci =

Which Javascript functional library: Underscore or wu.js or Functional or …? [closed]

守給你的承諾、 提交于 2019-12-21 04:38:27
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I am building a node.js app and wondering which javascript lib to add to my repertoire. Current short list includes: Underscore wu

Is it possible to filter array values by multiple values by using underscore?

北慕城南 提交于 2019-12-21 04:11:13
问题 I have the following array of values: [ { id: 1, field: 'map' }, { id: 2, field: 'dog' }, { id: 3, field: 'map' } ] I need to find out elements with field equals dog and map . I know I could use the _.filter method and pass an iterator function, but what I want to know is whether or not there's a better solution to this issue where I could pass search field and possible values . Could someone provide a better way of doing so? EDIT: : I could use the following approach: _.where(array, {field:

Javascript Multi-line string and Unexpected Token ILLEGAL

拟墨画扇 提交于 2019-12-21 04:10:24
问题 I have a multi-line string template in it's own separate js file for use in underscore js. However, no matter how I escape the line breaks I still get an: Uncaught SyntaxError: Unexpected token ILLEGAL On line 1 of the file when it's loaded into the browser. App.Templates['template1'] = '\ <div data-role="page" data-theme="c" id="" data-title="">\ <div data-role="content" class="subnav">\ <table id="day-table" cellpadding="0" cellspacing="0" border="0">\ <thead class="ui-body-a">\ <tr>\ <th>T

javascript - lodash - create a unique list based on multiple attributes

最后都变了- 提交于 2019-12-21 03:49:41
问题 My collection looks like this. var list = [{id:'12345', sequence:null}, {id:'12346', sequence:null}, {id:'12347', sequence:null}, {id:'12348', sequence:1}, {id:'12348', sequence:2}, {id:'12349', sequence:1}, {id:'12349', sequence:1}]; I am trying to get a unique list so that object with same id AND sequence will only return one of the objects (we have 2 here- {id:'12349', sequence:1}) my code var uniqueList = _.uniq(list, function(obj) { return obj.id && obj.sequence; }); can we use lodash

javascript - lodash - create a unique list based on multiple attributes

余生颓废 提交于 2019-12-21 03:49:29
问题 My collection looks like this. var list = [{id:'12345', sequence:null}, {id:'12346', sequence:null}, {id:'12347', sequence:null}, {id:'12348', sequence:1}, {id:'12348', sequence:2}, {id:'12349', sequence:1}, {id:'12349', sequence:1}]; I am trying to get a unique list so that object with same id AND sequence will only return one of the objects (we have 2 here- {id:'12349', sequence:1}) my code var uniqueList = _.uniq(list, function(obj) { return obj.id && obj.sequence; }); can we use lodash