underscore.js

underscore.js with two arrays

允我心安 提交于 2019-12-25 03:40:52
问题 I have the following array: ['aaa', 'bbb', 'ccc', 'ddd'] My goal is to remove from it unexpected values: I tried to do it with underscore without function like below: _.without(['aaa', 'bbb', 'ccc', 'ddd'], 'bbb', 'ccc'); It works fine, but unfortunately it does not work with array: _.without(['aaa', 'bbb', 'ccc', 'ddd'], ['bbb', 'ccc']); I've googled a bit and find the post underscore.js - Is there a function that produces an array thats the difference of two arrays? But in my case this one

Using Javascript object as object key

五迷三道 提交于 2019-12-25 03:18:08
问题 I am trying to devise a way to use a simple Javascript object (one level deep key value pairs) as a key to another object. I am aware that merely using the object without stringification will result in [Object object] being used as the key; see the following: Using an object as a property key in JavaScript (so this question is not a duplicate). There is a blog post about it that takes this into account and also accounts for the need to sort by object key since their order is not guaranteed,

MeteorJS and UnderscoreJS: grouping results to plot points on a Google Map

 ̄綄美尐妖づ 提交于 2019-12-25 02:41:12
问题 I am working on a small MeteorJS app that plots points on a map based on popular areas for work. I have this: Template.list.jobs = function() { if(Session.get('currentIndustryOnet')) { jobs = Jobs.find({onet: Session.get('currentIndustryOnet')}).fetch(); // Session.set('jobCount', jobs.count()); var cnt = _.groupBy(jobs, 'address'); console.log(cnt); return Pagination.collection(jobs); } else { jobs = Jobs.find() Session.set('jobCount', jobs.count()); return Pagination.collection(jobs.fetch()

Error 'Uncaught SyntaxError: Unexpected token < in underscore.js'

最后都变了- 提交于 2019-12-24 19:22:54
问题 Here's my code, HTML: <script type="text/html" id="test_temp"> <div class="row" id="opportunityList"> <% _.each(ops, function(option){ <div class="span12"> <div class="well basicInformation"> <div class="row"> <div class="span4 opportunityName" > <h4><%= option.company_name %></h4> </div> <div class="span4 pull-right"> <ul class="inline"> <li class="dealGrade"> <span><%= option.dealGrade %></span> <span class="subheader">Deal Grade</span> </li> <li class="estimateddevices"> <span><%= option

Lowercase of object value in array with underscore js

╄→尐↘猪︶ㄣ 提交于 2019-12-24 14:04:37
问题 Let's say I have this JSON data in a data variable [{"id":"1","module_id":"1","title":"Test", "start_date":"2012-11-12" "end_date":"2012-11-18"}, {"id":"8","module_id":"1","title":"This is OK", "start_date":"2013-01-14","end_date":"2013-01-31"}] How do I use underscore.js to get following result? [{"id":"1","module_id":"1","title":"test", "start_date":"2012-11-12","end_date":"2012-11-18"}, {"id":"8","module_id":"1","title":"this is ok", "start_date":"2013-01-14","end_date":"2013-01-31"}] Can

callback function with underscore _.each()

早过忘川 提交于 2019-12-24 12:19:26
问题 I'm trying to implement an _.each() (that I wrote) inside another function and I keep getting "undefined" returned to me. I'm trying to use _.each() to apply a test function to an array. I know this a simple callback syntax issue, but its perplexing me. thanks in advance from a noob. here's my function: _.filter = function(collection, test) { _.each(collection, test()); }; this returns 'undefined' this is the array i'm passing as 'collection': [1, 2, 3, 4, 5, 6] this is the function i'm

Filter duplicate collection objects (case-insenstive and trim) by property value with underscore

时间秒杀一切 提交于 2019-12-24 12:05:51
问题 I'm looking for a way to filter / reject objects in a collection based on the value of a chosen property. Specifically I need to filter out objects that contain duplicate values for that chosen property. I need to convert the property value to lower case and trim the whitespace. I already have my method for removing the duplicates but I can't figure out how to include the lowercase conversion and the trim. removeDuplicates: function (coll, attr) { var uniques = _.map(_.groupBy(coll, function

Can't access variable inside same class

こ雲淡風輕ζ 提交于 2019-12-24 07:29:33
问题 var BooksView = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { alert(this.myVar); } }); and my router: var BookRouter = Backbone.Router.extend({ routes: { '': 'index', 'list/:id': 'list' }, index: function(){ var booksView = new BooksView({ el: ".content", myVar: "Example 1" }); }, list: function(id){ var booksView = new BooksView({ el: ".content", myVar: "Example 2" }); } }); How do I access the variable? this.myVar doesn't seem to be working, even

Underscore sortBy, groupBy with count of grouped arrays

[亡魂溺海] 提交于 2019-12-24 07:12:31
问题 I'm having a bit of trouble manipulating an array of objects using underscore. I am almost there but have got stuck on attempting to combine countBy within the underscore chain. I have the following array of objects (simplified): var messages = [ { id: 20, messageHeaderId: 6, content: "Message 1", dateOfMessage: "2017-08-16T12:34:49.403974", sender: "Diane" }, { id: 22, messageHeaderId: 6, content: "Latest message from MH6", dateOfMessage: "2017-08-16T12:38:49.403974", sender: "Diane" }, { id

Best way to group and sort an array of objects

百般思念 提交于 2019-12-24 04:16:32
问题 I have the following demo data. var demoData= [{"EntryGroupDate":"November 2013", "DisplayName": "Hans Meier (November)", "EntryGroupDateSort": 11}, {"EntryGroupDate":"August 2013", "DisplayName": "Franz Mueller (August)", "EntryGroupDateSort": 8}, {"EntryGroupDate":"November 2013", "DisplayName": "Franz Huber (November)", "EntryGroupDateSort": 11}, {"EntryGroupDate":"Juli 2013", "DisplayName": "Franz Schmidt (Juli)", "EntryGroupDateSort": 7} ]; What would be the best way to group them first