underscore.js

Update if exists or add new element to array of objects - elegant way in javascript + lodash

人走茶凉 提交于 2020-01-19 12:16:43
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description:

group array with sub array

孤街浪徒 提交于 2020-01-17 04:36:05
问题 I'm trying to find the easiest way to take an array that has like information and create an array with sub array. The newly created parent array should also sum the values that are found in the sub-array. This is the equivalent of an SQL statement that uses sum/group by. This is the original array [ { 'userId':1, 'userName':'bob', 'category':'shoes', 'count':2 }, { 'userId':1, 'userName':'bob', 'category':'rocks', 'count':4 }, { 'userId':1, 'userName':'bob', 'category':'bags', 'count':3 }, {

binding events to dynamic objects in underscore/backbone

风格不统一 提交于 2020-01-16 04:21:29
问题 I'm trying to figure out how to listen for custom events on objects which have not been prototyped or are not dom objects in underscore.js/backbone.js. for example: //this is inside a view object play: function(arg) { this.a = this.image(this.model.a); this.a.bind("ready",start,this);//<--- causes error this.b = this.image(this.model.b); this.b.bind("ready",start,this);//<--- causes error function start() { // do some stuff in here } //some more stuff }, image: function(args) { // load the

Create Multilevel groups from an Array of Objects

会有一股神秘感。 提交于 2020-01-16 03:54:08
问题 I have following type of data data = [{ "grp" : ["A", "B", "C"], "val" : [1, 2, 3] }, { "grp" : ["A", "B", "D"], "val" : [2, 3, 4] }, { "grp" : ["A", "C", "C"], "val" : [1, 3, 5] }, { "grp" : ["B", "Y", "E"], "val" : [1, 3, 2] } ] I want to make groups like these groups = { "A": { "B": { "C": [ [1, 2, 3], [2, 3, 4] ], "D": [ [1, 2, 3], [2, 3, 4] ] }, "C": { "C": [ [1, 3, 5] ] } }, "B": { "Y": { "E": [ [1, 3, 2] ] } } } grp array can be have maximum of 3 elements. I tried using underscorejs

How to groupby and get average using underscorejs

浪尽此生 提交于 2020-01-15 07:50:26
问题 How to groupBy category and get the average using underscore? I have an array of objects. It should be grouped by category and average for Analytics is calculated from the val property ie., 1+2 => 3. 3/total number of categories. So, 3/2 => 1.5 Expected Output: { Analytics: 1.5 } [{ area:"Digital", category:"Analytics", qId:"wRHmpHHGzrYLsCEJ3", type:"Reorganize", userId:"M4JEJGiPZ8e9om9A", val:1 }, { area:"Digital", category:"Analytics", qId:"wRHmpHHGzrYLsCEJ3", type:"Reorganize", userId:

underscore.js, global object named “exports” and livefyre javascript API - integration conflict

余生颓废 提交于 2020-01-15 03:48:06
问题 I'll try to summarize a few steps and ask a question after underscore.js failed to install when developing a 3rd-party javascript component (as described here: Underscore.js and noconflict). my code included the following line to avoid collisions of different versions of underscore.js : window.$MyUS = _.noConflict(); when looking into the underscore.js code, it was found that if exports or module.exports is defined, than the _ variable will become a member of exports or module.exports . It

Backbone collection sortBy

会有一股神秘感。 提交于 2020-01-14 10:40:08
问题 I make my first backbone app and get some problems with collection sorting. After using this var SortedFriends = MyFriends.sortBy(function(friend) { return friend.get("uid"); }); console.log(SortedFriends) show that SortedFriends contains sorted models, but when i try to use collection functions like 'SortedFriends.each' or 'SortedFriends.at' it make error: TypeError: SortedFriends.each is not a function. Code: var Friend = Backbone.Model.extend({}); var Friends = Backbone.Collection.extend({

lodash/underscore; compare two objects and remove duplicates

白昼怎懂夜的黑 提交于 2020-01-13 11:24:26
问题 As you can see in the image below, I have some returned json data with three objects; each contains a clients id => data. exact_match : {104} match_4 : {104, 103} match_2 : {104, 103, 68} How can I "trim" or remove the duplicate objects based on previous ones? something like: exact_match : {104} match_4 : {103} match_2 : {68} I tried _.difference but did not work (Maybe because it is for arrays not objects?): var exact_match = data.exact_match, match_four_digits = _.difference(data.match_4,

Dynamic nested for loops to be solved with recursion

雨燕双飞 提交于 2020-01-13 10:33:29
问题 I'm trying to get a result looking something like this: Miniors | Boys | 54kg - 62kg where every value delimited by a pipe | comes from an array containing a certain "type of restriction". For example: ageGroups, genders, weightClasses (as seen above). The way I'm able to get this result right now is if I hard code the nested forEach-loops (using underscorejs), but this means I have to now how many arrays I have to loop over to get wanted result. This works "fine": var categories = []; _.each

Loading templates with backbone js

喜你入骨 提交于 2020-01-12 14:05:12
问题 I'm starting in javascript development, and did a simple project with node.js as a rest API and a client using backbone, everything look perfectly till I want to get my templates out of my js. I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better: A .js file with a var with the html code pros -> easy to load, easy to pass to underscore to compile it. cons -> scape every single line. app.templates.view = " \ <h3>something