underscore.js

Javascript underscore array to object

↘锁芯ラ 提交于 2019-12-08 14:50:25
问题 Is there an easy/clean way using Underscore to turn this [ { id: 'medium', votes: 7 }, { id: 'low', votes: 9 }, { id: 'high', votes: 5 } ] Into { 'low' : 9, 'medium' : 7, 'high' : 5 } 回答1: var data = [ { id: 'medium', votes: 7 }, { id: 'low', votes: 9 }, { id: 'high', votes: 5 } ]; You can do this with _.map, _.values and _.object, like this console.log(_.object(_.map(data, _.values))); # { medium: 7, low: 9, high: 5 } Explanation We use the map function to apply the values function (which

Sort items in array by more then one field with lodash

懵懂的女人 提交于 2019-12-08 14:29:52
问题 How can I sort an array of objects by more then one field using lodash. So for an array like this: [ {a: 'a', b: 2}, {a: 'a', b: 1}, {a: 'b', b: 5}, {a: 'a', b: 3}, ] I would expect this result [ {a: 'a', b: 1}, {a: 'a', b: 2}, {a: 'a', b: 3}, {a: 'b', b: 5}, ] 回答1: This is much easier in a current version of lodash (2.4.1). You can just do this: var data = [ {a: 'a', b: 2}, {a: 'a', b: 1}, {a: 'b', b: 5}, {a: 'a', b: 3}, ]; data = _.sortBy(data, ["a", "b"]); //key point: Passing in an array

Combining object arrays ({a:a,b:b, c:c},{a:a,d:d,c:c}) to ({a:a,b:c,d:c})

僤鯓⒐⒋嵵緔 提交于 2019-12-08 13:35:37
问题 I have a list of data that I'm getting that I need to convert and "pivot" to turn one column into multiple columns. The list of objects I have looks like this: var list = [ { "date": "2016-05-31", "action": "test", "value": 10 }, { "date": "2016-05-31", "action": "run", "value": 8 }, { "date": "2016-06-01", "action": "delete", "value" : 2 }, { "date": "2016-06-01", "action": "test", "value": 5 }, ] I need to convert this for a charting library so that it looks like this: var list = [ { "date"

Underscore: Remove object having some duplicate properties

对着背影说爱祢 提交于 2019-12-08 12:41:51
问题 can somebody help me remove object from array having some duplicate properties. var data = [{ "IDPOSITION": "1", "LATITUDE": "5.35961", "LONGITUDE": "-3.10095", "IDUSAGER": "1", "DATECREATION": "2013-10-12 21:53:09" }, { "IDPOSITION": "2", "LATITUDE": "5.35961", "LONGITUDE": "-4.00095", "IDUSAGER": "1", "DATECREATION": "2013-10-12 21:53:51" }, { "IDPOSITION": "3", "LATITUDE": "5.35961", "LONGITUDE": "-4.00095", "IDUSAGER": "1", "DATECREATION": "2013-10-12 21:53:53" }]; I need to remove

How can accomplish For-Each loop from the underscore template to mustache

我的梦境 提交于 2019-12-08 12:33:21
问题 I have an underscore template and I have to use Mustache to render it. Below is my underscore template: <div id="sub-account"> <p>something</p> <table> <tr><td>Name</td> </tr> <tbody> <% _.each(accountList, function(account) { %> <tr> <td><%= account.get('name') %></td> </tr> <% }) %> </tbody> </table> </div> Im using a mustache as my main view to render a list. How can I loop through the code to render as a mustache template? Backbone Collection: var SubAccountCollection = Backbone

Syntax highlight javascript templates within <script type='text/html'> elements

可紊 提交于 2019-12-08 11:09:19
问题 In my Ruby on Rails templates I write underscore templates within <script type='text/html'> elements. I'm using rails.vim and other plugins, but syntax highlighting and identation rules don't work for the htmls inside these elements. 回答1: Put it in a partial. That also keeps the page with all the html scripts from growing into a monster. <script type="text/x-underscore"> <%= render 'my_partial_with_syntax_highlighting' %> </script> Ditto for using something other than text/html for content

count category by userID using Underscorejs

China☆狼群 提交于 2019-12-08 10:52:47
问题 Countby category based on userId using undersocrejs. Please refer the below script, its printing the value "Technology:2" & "Analytics:1". Expected answer: "Technology:1" & "Analytics:1" because both the 'Technology' objects are the same userId ie. 1 arrayFlatten = [ { area:"Digital", category:"Technology", userId:1, weightedAverage:10 }, { area:"Digital", category:"Technology", userId:1, weightedAverage:20 }, { area:"Digital", category:"Analytics", userId:2, weightedAverage:30 } ] var types

Remove li from Ul backbone

[亡魂溺海] 提交于 2019-12-08 08:21:28
Iam trying to build a very simple ul where u have a input box and add button , on clicking add button , text from input is appended to ul this is my code : HTML : <body> <input type="text" id="name"> <button id="add">Add</button> <ul id="mylist"></ul> JS : $(function(){ var myCollection = Backbone.Collection.extend(); var myView = Backbone.View.extend({ el:$('body'), tagName:'li', initialize : function(e){ this.collection.bind("add",this.render,this); }, events:{ 'click #add' : 'addfoo', 'click .button':'removefoo' }, addfoo:function(){ var myname= $('#name').val(); $('#name').val(''); console

npm not install underscore packages

こ雲淡風輕ζ 提交于 2019-12-08 08:12:24
问题 I want to install packages with npm : my packages file is: "dependencies": { "express": "~3.3.6", "socket.io": "0.9.16", "jade": "~0.35.0", "less-middleware": "~0.1.12", "redis": "~0.8.4", "connect-redis": "~1.4.5", "longjohn": "~0.2.1", "mongoose": "~3.6.20", "json-stringify-safe": "~5.0.0" }, "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-uglify": "~0.2.2", "grunt-nodemon": "~0.1.0", "grunt-contrib-jshint": "~0.6.3", "grunt-contrib-less": "~0.7.0", "grunt-ngmin": "0.0.3" } } but i

Remove li from Ul backbone

被刻印的时光 ゝ 提交于 2019-12-08 07:58:17
问题 Iam trying to build a very simple ul where u have a input box and add button , on clicking add button , text from input is appended to ul this is my code : HTML : <body> <input type="text" id="name"> <button id="add">Add</button> <ul id="mylist"></ul> JS : $(function(){ var myCollection = Backbone.Collection.extend(); var myView = Backbone.View.extend({ el:$('body'), tagName:'li', initialize : function(e){ this.collection.bind("add",this.render,this); }, events:{ 'click #add' : 'addfoo',