underscore.js

Underscore gives error when bundling with Webpack

╄→尐↘猪︶ㄣ 提交于 2019-12-07 01:32:35
问题 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 // -------------- //

Chrome will not refresh local file when changing backbone template

随声附和 提交于 2019-12-07 01:09:35
问题 I'm working on a backbone application on Google Chrome (v19.0.1084.56) and when I am working on templates, both me (on Mac OSX Lion) and another developer (on Fedora) have to close the tab and reopen the file to see new changes. Forced refresh (cmd+R or f5) do not work. I am using these flags for chrome to work on the file: open /Applications/Google Chrome.app --args --allow-file-access-from-files --disable-web-security I'm driving myself crazy with this close/open process. Has anyone run

Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working?

半腔热情 提交于 2019-12-06 22:29:08
问题 I'm using Mongoose and I want to remove the _id property from my Mongoose instance before I send the JSON response to the client. Example: var ui = _.clone(userInvite); delete ui["_id"]; console.log(JSON.stringify(ui)); //still has "_id" property, why? The previous didn't work. However, if I do: var ui = JSON.parse(JSON.stringify(userInvite)); //poor man's clone delete ui["_id"]; console.log(JSON.stringify(ui)); //"_id" is gone! it works! I don't understand why calling delete on a cloned

Pluck specific javascript value from an object based on an array of indexes

余生颓废 提交于 2019-12-06 22:26:07
问题 Given a nested object like this: var cars = { "bentley": { "suppliers": [ { "location": "England", "name": "Sheffield Mines"} ] // ... } }; and an array like this ["bentley", "suppliers", "0", "name"] , is there an existing function that will pluck the deepest element, i.e. pluck_innards(cars, ['bentley', "suppliers", "0", "name"]) and that returns "Sheffield Mines". In other words, is there a function (which I will name deep_pluck ) where deep_pluck(cars, ['bentley', 'suppliers', '0', 'name'

Mapping two (or more) arrays into one with underscore.js

蓝咒 提交于 2019-12-06 21:28:26
问题 I'd need to add element-wise several arrays. That is, I have several arrays of equal lenght, and I'd need just one with the same number of elements that are the sum of the inputs. Underscore has methods to fold all elements into one and to map every element using a function, but I can't find any way to combine two arrays piece wise. If my original arrays were [1,2,3,4,5,6] , [1,1,1,1,1,1] and [2,2,2,2,2,2] the result should be [4,5,6,7,8,9] . I know I can do it by iterating over the arrays,

Using underscore.js for null checking

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 19:39:05
问题 var name = "someName"; if(name !=null) { // do something } I am right now using http://underscorejs.org/#isNull, how would i do the same using underscore.js Does it give any slight improvement in terms of performance for such functions. 回答1: In underscore, you can use if(!_.isNull(name)) {} and in plain Javascript, you should use if(name !== null) {} You should avoid the loose inequality operator != because it does type coercion and undefined != null will return false . Using plain Javascript

How to get first n elements of an object with underscore?

只谈情不闲聊 提交于 2019-12-06 18:42:54
问题 How can I get the first n (for example 3) elements of an object with underscore? Thank you! 回答1: http://underscorejs.org/#first _.first allows you to pass a number to get the first n elements of an array. _.first([1,2,3], 2) // [1,2] If by object, you mean associative object, the values are not in any specified order. You could do _.first(_.values( { 'a' : 5, 'b' : 6, 'c' : 8 }), 2) // [5,6] but it is not guaranteed that the values you get out will be the 'first' ones, so I'm not sure when

Clean way to map an array in node.js or JavaScript

我只是一个虾纸丫 提交于 2019-12-06 18:06:40
问题 Let's say I have a function and an array. I want to modify the array by applying the function to each entry in the array. The function does NOT modify the value directly; it returns a new value. In pseudo-code, for (entry in array) { entry = function(entry); } There are a couple ways to do this that occurred to me: for (var i = 0; i < arr.length; i++) { arr[i] = fn(i); } Or, since I am using node.js and have underscore built in: arr = _.map(arr, fn); But this both seem a little clunky. The

Converting Json object in to array

淺唱寂寞╮ 提交于 2019-12-06 16:52:52
From the below Json, I am trying to build an array as shown below. "Issues": [ { "Id": null, "Key": null, "Values": [ { "Key": "Display Name", "Value": "Rya" }, { "Key": "UserName", "Value": "RH" }, { "Key": "Count", "Value": "350" } ] }, { "Id": null, "Key": null, "Values": [ { "Key": "Display Name", "Value": "Mike" }, { "Key": "UserName", "Value": "ML" }, { "Key": "Count", "Value": "90" } ] } ] Desired Array: { "Display Name": 'Rya', "UserName" : "RH", value: 350 }, { "Display Name": 'Mike', "UserName" : "ML", value: 90 } Here, there can be any number of fields in the array, but not confined

npm not install underscore packages

寵の児 提交于 2019-12-06 15:43:04
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 have this error : npm http 304 https://registry.npmjs.org/underscore.string npm ERR! Error: No