underscore.js

Joins in Javascript

我是研究僧i 提交于 2019-12-17 23:37:18
问题 I have 2 lists of objects: people = [{id: 1, name: "Tom", carid: 1}, {id: 2, name: "Bob", carid: 1}, {id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}]; cars= [{id: 1, name: "Ford Fiesta", color: "blue"}, {id: 2, name: "Ferrari", color: "red"}, {id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}]; Is there a function (possibly in Angular, JQuery, Underscore, LoDash, or other external library) to do a left join in one line on these? Something like: peoplewithcars

Best way to asynchronously load underscore templates

耗尽温柔 提交于 2019-12-17 23:30:12
问题 I'm planning to use backbone.js and underscore.js for creating website, and I will have lots of underscore templates: <script type="text/template" id="search_template"> <p id="header"> //header content will go here </p> <p id="form"> <label>Search</label> <input type="text" id="search_input" /> <input type="button" id="search_button" value="Search" /> </p> <p id="dynamic_date"> //dynamic data will be displayed here </p> </script> Of course my templates will be much more complicated. Since I

Render html table with underscore template engine using a backbone complex model

匆匆过客 提交于 2019-12-17 22:35:58
问题 I'm trying to render an html table using underscore template engine. First I have the JSON response from the server like this { CurrentModel: { Heading: "Home", Id: "pages/193", Metadata: { Name: "Home", Title: null, Keywords: null, Description: null, DisplayInMenu: true, Published: "/Date(1334499539404)/", Changed: "/Date(1334499539404)/", ChangedBy: "Marcus", IsPublished: true, IsDeleted: false, Slug: null, Url: null, SortOrder: 0 }, Parent: null, Children: [ "pages/226", "pages/257" ] },

One liner to flatten nested object

不羁的心 提交于 2019-12-17 19:19:46
问题 I need to flatten a nested object. Need a one liner. Not sure what the correct term for this process is. I can use pure Javascript or libraries, I particularly like underscore. I've got ... { a:2, b: { c:3 } } And I want ... { a:2, c:3 } I've tried ... var obj = {"fred":2,"jill":4,"obby":{"john":5}}; var resultObj = _.pick(obj, "fred") alert(JSON.stringify(resultObj)); Which works but I also need this to work ... var obj = {"fred":2,"jill":4,"obby":{"john":5}}; var resultObj = _.pick(obj,

Group By and Sum using Underscore/Lodash

十年热恋 提交于 2019-12-17 18:44:18
问题 I have JSON like this: [ { platformId: 1, payout: 15, numOfPeople: 4 }, { platformId: 1, payout: 12, numOfPeople: 3 }, { platformId: 2, payout: 6, numOfPeople: 5 }, { platformId: 2, payout: 10, numOfPeople: 1 }, ] And I want to Group it by platformId with sum of payout and numOfPeople . I.e. in result I want JSON like this: [ "1": { payout: 27, numOfPeople: 7 }, "2": { payout: 16, numOfPeople: 6 } ] I tried to use underscore.js 's _.groupBy method, and it groups fine, but how I can get the

Sorting an Array of JavaScript Objects a Specific Order (using existing function)

倾然丶 夕夏残阳落幕 提交于 2019-12-17 17:36:12
问题 Given an array of objects: { key: "a", value: 42 }, { key: "d", value: 28 }, { key: "c", value: 92 }, { key: "b", value: 87 } and an array of keys: ["c", "a", "b", "d"] Is there a ECMAScript function or a 3rd-party JavaScript library that lets you sort - in one line/function call - the first array of objects, to match the order of the keys specified in the second array, such that the result is: { key: "c", value: 92 }, { key: "a", value: 42 }, { key: "b", value: 87 }, { key: "d", value: 28 }

$q promise with Underscore _each

╄→гoц情女王★ 提交于 2019-12-17 16:58:41
问题 So I have a method in a angularjs server that is calling a method that returns a promise for each method in a array. I am using underscore _each to loop through the array. I want to wait until the entire array is processed before I call the final line of code in the method.. So... function ProcessCoolStuff(coolStuffs) { var stuff = []; _.each(coolStuffs, function(coolStuff) { //Some method using $q to return makeStuffCooler(coolStuff).then(function(coolerStuff) { stuff.push(coolerStuff); });

Sorting by date with underscore.js or just plain JS

丶灬走出姿态 提交于 2019-12-17 16:29:31
问题 I have an array of objects that have a 'date' string property. ie: [ { id: 1, startDate: '2011-4-22' }, { id: 2, startDate: '2012-3-15' }, { id: 3, startDate: '2011-4-22' }, { id: 4, startDate: '2012-2-10' } ] I just want to convert the date strings to a date and sort them by startDate DESC. Can someone please tell me how to do this with teh underscore.js _sortBy method or even just plain javascript will do. Thanks! 回答1: An Underscore solution could look like this: a = [ /* ... */ ]; function

uncaught TypeError: Cannot call method 'replace' of undefined backbone.js

谁说胖子不能爱 提交于 2019-12-17 12:47:24
问题 I 'm trying to develop a simple RSS app using backbone.js. I 'm using this backbone.js tutorial. I 'm getting the following error, on line 2(template), when defining the template. Can someone also tell me why is tagName: "li" defined in the tutorial? uncaught TypeError: Cannot call method 'replace' of undefined backbone.js Javscript window.SourceListView = Backbone.View.extend({ tagName:"li", template: _.template($('#tmpl_sourcelist').html()), initialize:function () { this.model.bind("change"

What is the difference between these Backbone/Underscore .bind() methods?

佐手、 提交于 2019-12-17 10:42:57
问题 window.SomeView = Backbone.View.extrend({ initialize1: function() { _.bindAll(this, 'render'); this.model.bind('change', this.render); }, initialize2: function() { this.model.bind('change', _.bind(this.render, this)); }, initialize3: function() { _.bind(this.render, this); this.model.bind('change', this.render); }, }); With help from some SO members, I was able to get my test project working with binding methods initialize1 and initialize2; what I don't understand is why initialize3 doesn't