Tag: lodash

JSHint和下划线'_'

运行jshint时出现以下错误; line 4 col 5 Redefinition of '_'. 代码是抱怨是; var _ = require('lodash'); jshint在项目中 { "node": true, "esnext": true, "bitwise": true, "eqeqeq": true, "immed": true, "latedef": "nofunc", "newcap": true, "noarg": true, "regexp": true, "undef": true, "smarttabs": true, "asi": true, "debug": true, "globals": { "angular": false, "_": false } }

运行随机数字并保持状态

我使用下面的代码从指定的范围内获得随机值 var provideRanges = function(){ var aa = []; _.times(3, function(i) { console.log(i); let num = _.random(10, 20) console.log("num = :" + num) debugger; aa.push(num); console.log("value is :" + aa[i]); }); } 当你调用这个函数的时候,这是工作的,并且提供了在指定范围内的3个数组,但是当我需要的时候,这个问题变得更加困难,如果我再次调用它,忽略从提供的数字中排除之前提供的数字(就像它提供10,11,12下一次这些数字不应该提供…),是否有更好的方法来做到这一点? 我尝试使用recoursive电话,但迷路:(任何想法如何做到这一点?

如何在lodash中打破_forforEach?

我不能打破_.forEach循环,帮我做到这一点。 _.forEach(oIncludedFileMap, function (aIncludedFiles, sKey) { if(aIncludedFiles == null){ break; } });

数组内使用lodash数组的联合

我怎样才能使用lodash数组内的数组? 例如: input: var x = [ [1,2,3,4], [5,6,7], [], [8,9], [] ]; 预期产出: x = [1,2,3,4,5,6,7,8,9]; 目前我的代码执行以下操作: return promise.map(someObjects, function (object)) { return anArrayOfElements(); }).then(function (arrayOfArrayElements) { // I tried to use union but it can apply only on two arrays _.union(arrayOfArrayElements); });

Javascript对象值的和数组

我会认为这是一个令人惊讶的常见和简单的问题,但我似乎无法find我在寻找什么 如果我有 var array = [{"a":4,"b":5, "d":6}, {"a":4, "c":5}, {"c":4}] 我如何总结对象得到 {"a":8,"b":5,"c":9, "d":6} 使用下划线,lodash或相当快速和简单的一个class轮 ?

与数组对象一起使用

我有一个可以包含相同对象types的子对象的数组,像这样: var exampleArray = [ { alias: 'alias1', children: [ { alias: 'child1' }, { alias: 'child2', children: [ { alias: 'child4' }, { alias: 'child5' } ] }, { alias: 'child3' } ] }, { alias: 'alias2' }, { alias: 'alias3', children: [ { alias: 'child6' }, { alias: 'child7' } ] } ]; […]

如果path有效,Lodash会返回值数组

我有以下对象,并在Node.js(我只是在terminal中运行node )lodash“查询”: var obj = { a: [{ b: [{ c: "apple" }, { d: "not apple" }, { c: "pineapple" }] }] }; > _.get(obj, "a[0].b[0].c") 'apple' > _.get(obj, "a[0].b[1].c") undefined > _.get(obj, "a[0].b[2].c") 'pineapple' 我的问题是:有没有办法返回的path被发现有效的值的数组? 例: > _.get(obj, "a[].b[].c") ['apple', 'pineapple']

内在像lodash合并

我想在node.js中做一个与lodash合并的对象。 合并很好,因为它不会覆盖导致未定义的属性对象。 不过,我希望它的手段只覆盖目标对象中存在的对象。 看下面的例子: var e1 = { name: 'Jack', surname: 'Root' }; 合并 var e2 = { name: 'Rex', surname: undefined, age: 24 }; 当前结果: { name: 'Rex', surname: 'Root', age: 24 } 结果我想: { name: 'Rex', surname: 'Root' } 所以我想得到的是源对象只覆盖两者中存在的属性,只有当它们不是未定义的。 我试图寻找其他function,但只发现合并和分配做类似的事情。 但唉,这不是我想要的。 这个背后的全部想法是build立一些方法,将从Web窗体中获取对象字段,然后绑定到一个mongoose.js模型对象进行持久化。 这是为了避免总是手动将每个属性绑定到另一个对象。

Lodash地图并返回唯一

我有一个lodashvariables; var usernames = _.map(data, 'usernames'); 这产生以下; [ "joebloggs", "joebloggs", "simongarfunkel", "chrispine", "billgates", "billgates" ] 我怎样才能调整lodash语句,以便它只返回一个唯一值的数组? 例如 var username = _.map(data, 'usernames').uniq();

如何避免在JavaScript应用程序中丢失“this”上下文?

这是我的代码示例: function QueueService() { var key = Config.get("AWS.accessKeyID"); var secret = Config.get("AWS.secretKey"); var credentials = new AWS.Credentials(key, secret, sessionToken = null); this._sqs = new Promise.promisifyAll(new AWS.SQS({apiVersion: '2012-11-05', region: "us-west-2", endpoint: "sqs.us-west-2.amazonaws.com", credentials: credentials})); } QueueService.prototype.FillBirdQueue = function(birds) { var self = this; birds.forEach(function(bird_batch) { var params = { Entries: bird_batch, QueueUrl: Config.get("AWS-Queue.Birds") }; return self._sqs.sendMessageBatchAsync(params); […]