underscore.js

The order of operators in JavaScript (|| ,&&)

安稳与你 提交于 2019-12-05 12:02:54
I am reading the source code of Underscore.js, then something confused me: // Its code, check the passed-in parameter obj _.isObject = function(obj) { var type = typeof obj; return type === 'function' || type === 'object' && !!obj; }; I am confused about the operator order of expression. I think the operator precedence in return type === 'function' || type === 'object' && !!obj; will be from left to right ; I mean equal to : return (type === 'function' ) || ( type === 'object' && !!obj); if type equal function return true ; else operate type === 'object' && !!obj ; if type equal object return

Group array values in group of 3 objects in each array using underscore.js

佐手、 提交于 2019-12-05 11:53:35
Below is an array in which I have to group 3 values in each object var xyz = {"name": ["hi","hello","when","test","then","that","now"]}; Output should be below array - ["hi","hello","when"]["test","then","that"]["now"] Gayathri Mohan Hi please refer this https://plnkr.co/edit/3LBcBoM7UP6BZuOiorKe?p=preview . for refrence Split javascript array in chunks using underscore.js using underscore you can do JS var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"]; var n = 3; var lists = _.groupBy(data, function(element, index){ return Math.floor(index/n); });

Filter array based on an array of index

五迷三道 提交于 2019-12-05 10:42:29
First I apologize if it's a duplicate (I searched but did not find this simple example...), but I want to select elements of arr1 based on the index in arr2 : arr1 = [33,66,77,8,99] arr2 = [2,0,3] I am using underscore.js but the 0 index is not retrieved (seems to be considered as false ): res = _.filter(arr1, function(value, index){ if(_.contains(arr2, index)){ return index; } }); Which returns: # [77, 8] How could I fix this, and is there a simpler way to filter using an array of indexes? I am expecting the following result: # [77, 33, 8] The simplest way is to use _.map on arr2 , like this

Uncaught TypeError: Cannot call method 'replace' of null

天涯浪子 提交于 2019-12-05 09:54:26
If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well >> _.template($('#pranks-list').html()) function (a){return e.call(this,a,b)} app.js // Views window.PranksListView = Backbone.View.extend({ template: _.template($('#pranks-list').html()) }); Index.html <script type="text/html" id="pranks-list"> <li><a href='#pranks/<%= id %>'><%= name %></a></li> </script> </body> Why I get this error on this line?? template: _.template($('#pranks-list').html()) It's hard to tell without seeing the whole code, but you are probably trying to run _.template($('#pranks-list'

Iterate over object attributes and modify them

回眸只為那壹抹淺笑 提交于 2019-12-05 09:41:11
问题 Underscore.js provides _.each and _.map on collections, which is nice, but I need to iterate over all attributes of my object. I need to modify the values and preserve the keys. E.g. I've got something like: {a:1, b:2, c:3} and I need to perform an operation that changes the value but keeps the keys. Let's say, I'll calculate squares, I should get {a:1, b:4, c:9} . The question is: how to do that using underscore (not interested in vanilla javascript)? I'd love a method like: var a = {a:1, b

Underscore bind vs jQuery.proxy vs Native bind

倾然丶 夕夏残阳落幕 提交于 2019-12-05 08:10:17
I have some context issues in callback. I googled and found few option: Native bind - not supported by old browsers JQuery proxy underscore bind I'll definitely use native bind if I don't have to support old browsers. Is there any significant difference between these one should be aware of? Could these be used as an alternate to call/apply? Adam The call and apply methods invoke a function. The bind method returns a function object that can be used a reference (for use in callbacks, for instance). Therefore, bind and call/apply don't generally have the same use cases. That being said, MDN has

How can I add a delay inside each iteration of an _.each loop in underscore.js?

扶醉桌前 提交于 2019-12-05 08:04:53
How can I add a delay inside each iteration of an _.each loop to space out the calling of an interior function by 1 second? _.each(this.rows, function (row, i) { row.setChars(msg[i] ? msg[i] : ' '); }); You don't need extra IIFE _.each(this.rows, function (row, i) { setTimeout(function () { row.setChars(msg[i] ? msg[i] : ' '); }, 1000 * i); }); since you're not doing it in an explicit for loop. Found an answer, just add a self invoking function inside the _.each loop with a timeout that continues to scale based on the number of iterations of the loop. Here's a working example (Edited to remove

Place client-side JavaScript templates in HTML or JavaScript?

你。 提交于 2019-12-05 07:55:11
Should client-side templates like the following (using underscore's templating engine): <p class="foo"><%= bar %></p> be placed in an separate HTML file, or a separate JavaScript file? I know it could work both ways. For example, a JavaScript file could just contain a list of string variables like so: var cute = '<p class="the"><%= unicorn %></p>'; var fb = '<p class="new-design"><%= sucks %></p>'; But I have also seen the following: <script type="text/template" id="omg-template"> <span id="swag-name"><%= name %></span> </script> Just from a Separation of Concerns standpoint, where does the

what is it used for _.once in underscore?

早过忘川 提交于 2019-12-05 06:13:51
I just look at the once API of source in underscore.js, then wandering what is it the used for in the method, it seems doing nothing: func = null the source: _.once = function(func) { var ran = false, memo; return function() { if (ran) return memo; ran = true; memo = func.apply(this, arguments); func = null; return memo; }; }; What the function does can be found in the documentation : Creates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call. Useful for initialization functions,

Underscore gives error when bundling with Webpack

核能气质少年 提交于 2019-12-05 04:56:40
I am trying to rewrite old application that uses require.js to use es6 imports. One of the used libraries are Backbone and Underscore. To create one big bundle and precompile es6 to es5, I use Webpack with babel-loader. Bundle gets created but when I load it in browser I am getting following error: Uncaught TypeError: Cannot read property '_' of undefined It seems that 'this' in Underscore is undefined in created bundle.js so root._ gives me error. // Baseline setup // -------------- // Establish the root object, `window` in the browser, or `global` on the server. var root = this; // Save the