underscore.js

Destructively map object properties function

 ̄綄美尐妖づ 提交于 2019-12-11 02:43:09
问题 I was looking for an example or solution for mapping or changing values of an object 'destructively' instead of returning a new object or copy of the old object. underscore.js can be used since the project already uses this third party library. 回答1: This is how one such solution could look like, using underscore: function mapValuesDestructive (object, f) { _.each(object, function(value, key) { object[key] = f(value); }); } an example mapper function: function simpleAdder (value) { return

Can't loop over backbone collection

强颜欢笑 提交于 2019-12-11 01:56:03
问题 It seems that I can't loop through a backbone collection. I've seen this topic in several threads, but none of those solutions help. render:function () { this.$el.html(this.template(this.model.attributes)); var that = this; console.log(this.projects.models); _.each(this.projects.models, function(model) { console.log(model); }); return this; } From this my console only shows Array[2] I would expect to see each model as well. Does anyone know what I'm doing wrong here ? 回答1: To get your

Merging two backbone collection and models into one object using underscore

廉价感情. 提交于 2019-12-11 00:57:35
问题 I have two backbone collections Categories and Items, Categories contains category models with id, name, and items (w/c contains item ids in string format separated by commas) and Items collection that contains item models(id, name, etc.). How do I merge them into one object that can be easily be rendered in a handlebars template Sample Structure: var Categories = [{'id':'1', 'category_name':'Burgers', 'category_items':'1,2'},{'id':'2','category_name':'Drinks','category_items':'3'}]; var

Why the name 'underscore' or 'lodash'?

喜你入骨 提交于 2019-12-11 00:27:08
问题 Why are these libraries named after _ ? Is there some significance behind it or the reason is "Just because we can" ? As far as i know, underscore and lodash do a lot of similar stuff. Also, both the names point to _ Even their variable names are _ So is there some relation of _ with the working of these libraries? Or its just a name? 回答1: Lodash From my understanding of the history of the two, lodash was meant as a lightweight replacement for underscore. So lodash is effectively a play on

RequireJS - How to configure Underscore before it's loaded by Backbone?

戏子无情 提交于 2019-12-11 00:02:48
问题 I'd like to know how to load Underscore and Backbone using RequireJS and be able to configure Underscore before it's passed to Backbone. I've seen this popular question on StackOverflow about this similar topic, but I haven't been able to figure out how to get Underscore to be configured for both of the following situations: Underscore is required by itself Underscore is loaded by Backbone So my question is the following: How can I configure Underscore using RequireJS when required by itself,

Create html structure which will allow only 3 div elements in each li. In React + underscore.js

夙愿已清 提交于 2019-12-10 23:56:45
问题 This is bit copy of How to create html structure which will allow only 3 div elements in each li. In React + underscore.js Actually i am facing issue while iterating array in li. I have below array structure var xyz = [{'name': ['abc','xyz','test','test1','test2','test3','test4'] }]; and i want below output structure <ul> <li> <div><span>test3</span></div> <div><span>test4</span></div> <div><span>test5</span></div> </li> <li> <div><span>test6</span></div> <div><span>test7</span></div> <div>

using _.omit on mongoose User in node.js

ぐ巨炮叔叔 提交于 2019-12-10 21:50:02
问题 I have a mongoose User schema built like this: var UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } }, password: { type: String, required: true }, salt: { type: String, required: true} }); I want to be able to send this user object to the client side of my application but I don't want to sned the password or salt fields. So I added he following code to my user model module U serSchema.methods.forClientSide = function() { console.log('in UserSchema

What's the difference between using require in node console and use script tag in html

元气小坏坏 提交于 2019-12-10 20:42:58
问题 I'm new to the js world, this is really make me confused, yesterday I was started to cooperate underscore.js in my code, so I start to try it in REPL environment, I choose to use Node console, I came up with var _ = require (./underscore.js) , then everything works fine. Today I try to embed it in html by using <script> tag, it seems I don't need to manually var _ = underscore , object _ is already there. someone can explain why, and how to use npm to install underscore globally and require

emulating the behavior of findWhere() in python

我怕爱的太早我们不能终老 提交于 2019-12-10 20:41:27
问题 Underscore has a handy little function, findWhere() which can be used to find a certain structure in a list like myList = [ {'name': 'Thor'}, {'name': 'Odin'}, {'name': 'Freya'}, {'name': 'Skadi'} ]; findWhere(myList, {'name': 'Skadi'}); result: [{'name': 'Skadi'}] Better example: my_list = [ {'name': 'Thor', 'occupation': 'God of Thunder', 'favorite color': 'MY HAMMER'} {'name': 'Skadi', 'occupation': 'Queen of the Ice Giants', 'favorite color': 'purpz'} ] findWhere(my_list, {'name': 'Skadi'

Using debounce function in underscore

ぃ、小莉子 提交于 2019-12-10 20:27:30
问题 I use underscore.js to run task. _.debounce(task, 100) How stop executing _.debounce ? 回答1: _.debounce does not execute anything, so you cannot stop it. It returns a new function which takes care of the debouncing. If you don't want to use it anymore, just use the original function. Have a look at the documentation for more information. 回答2: I assume you want to slow down an expensive task, i've written a post here to explain the _.debounce() method behaviour. Cheers PS....archived post 来源: