标签: lodash

使用where方法的Backbone/Underscore链方法

我必须在这里找到一些简单的东西.

http://jsfiddle.net/v9mdZ/

我正在学习Backbone和Underscore/loDash,我正在努力学习chain.

我有以下代码,它按预期工作:

var ids = _.pluck(collection.where({'is_checked':true}), 'id');
Run Code Online (Sandbox Code Playgroud)

我尝试重构这个,使用chain如下:

var ids = collection.chain().where({'is_checked':true}).pluck('id').value();
Run Code Online (Sandbox Code Playgroud)

为什么重构代码不起作用?我用chain错了吗?

解决方案(详见下方)

不要使用wherechain.

backbone.js underscore.js lodash

14
推荐指数
1
解决办法
2772
查看次数

在JavaScript中将数组迭代为一对(当前,下一个)

Python中将列表迭代为对(当前,下一个)的问题中,OP有兴趣将Python列表迭代为一系列current, next对.我有同样的问题,但我想用最干净的方式在JavaScript中完成,也许使用lodash.

使用简单的for循环很容易做到这一点,但它感觉不是很优雅.

for (var i = 0; i < arr.length - 1; i++) {
  var currentElement = arr[i];
  var nextElement = arr[i + 1];
}
Run Code Online (Sandbox Code Playgroud)

Lodash几乎可以做到这一点:

_.forEach(_.zip(arr, _.rest(arr)), function(tuple) {
  var currentElement = tuple[0];
  var nextElement = tuple[1];
})
Run Code Online (Sandbox Code Playgroud)

在最后一次迭代中,这nextElement将是一个微妙的问题undefined.

当然,理想的解决方案只是一个pairwise只在必要时循环的lodash功能.

_.pairwise(arr, function(current, next) {
  // do stuff 
});
Run Code Online (Sandbox Code Playgroud)

是否有任何现有的库已经这样做了?或者是否有另一种很好的方法在JavaScript中进行成对迭代我还没有尝试过?


澄清:如果arr = [1, 2, 3, 4],那么我的pairwise函数将迭代如下:[1, 2], …

javascript iteration lodash

14
推荐指数
5
解决办法
8648
查看次数

JavaScript object.hasOwnProperty(proName)vs lodash _.has(obj,proName)函数

我正在讨论使用JavaScript Object.hasOwnProperty(propName)和lodash _.has(obj, proName)函数来确定对象是否具有属性.

对于简单的案例,哪个更有效?对于复杂的情况?对于所有情况?

有没有更好的图书馆,我没有提到过?

谢谢!

javascript properties lodash

14
推荐指数
1
解决办法
6500
查看次数

为什么lodash的.isObject,.isPlainObject的行为与"typeof x ==='object'"不同?

考虑以下:

var o1 = {}
var O = function () {
  return this
}
var o2 = new O()
var o3 = function() {}
var o4 = [o1, o1]

var output = [
    [_.isObject(o1), _.isObject(o2), _.isObject(o3), _.isObject(o4)], 
    [_.isPlainObject(o1), _.isPlainObject(o2), _.isPlainObject(o3), _.isPlainObject(o4)],
    [typeof o1 === 'object', typeof o2 === 'object', typeof o3 === 'object', typeof o4 === 'object'],
    [o1 instanceof Array, o2 instanceof Array, o3 instanceof Array, o4 instanceof Array]
]

/* outputs:

[
    [true,true,true,true],
    [true,false,false,false],
    [true,true,false,true],
    [false,false,false,true]
]

*/
Run Code Online (Sandbox Code Playgroud)

很明显,我们可以看到 …

javascript lodash

14
推荐指数
2
解决办法
9707
查看次数

运行grunt-bower抛出_.object不是函数错误

我正在尝试运行grunt-bower任务来复制我所有的凉亭组件.

Running "bower:dev" (bower) task
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for angular-mocks!
TypeError: _.object …
Run Code Online (Sandbox Code Playgroud)

javascript node.js gruntjs lodash

14
推荐指数
1
解决办法
481
查看次数

Lodash:是否可以使用具有异步功能的地图?

考虑这段代码

const response  = await fetch('<my url>');
const responseJson = await response.json();
responseJson =  _.sortBy(responseJson, "number");

responseJson[0] = await addEnabledProperty(responseJson[0]);
Run Code Online (Sandbox Code Playgroud)

addEnabledProperty扩展添加enabled属性的对象是什么,但这并不重要.功能本身效果很好

async function addEnabledProperty (channel){

    const channelId = channel.id;
    const stored_status = await AsyncStorage.getItem(`ChannelIsEnabled:${channelId}`);
    let boolean_status = false;
    if (stored_status == null) {
        boolean_status = true;
    } else {
        boolean_status = (stored_status == 'true');
    }

    return _.extend({}, channel, { enabled: boolean_status });
}
Run Code Online (Sandbox Code Playgroud)

有没有办法使用_.map(或另一个系统)循环通过整个responseJson数组来addEnabledProperty对每个元素使用?

我试过了:

responseJson = _.map(responseJson,  function(channel) {
            return addEnabledProperty(channell);
        }); …
Run Code Online (Sandbox Code Playgroud)

javascript async-await lodash

14
推荐指数
1
解决办法
1万
查看次数

Underscore.js - 获取唯一属性值

我最近才发现了underscore.js的强大功能,对我提出建议的方法仍然是新手:

我如何从中得到:

[
    [{
        "name": "Type 2",
        "id": 14
    }],
    [{
        "name": "Type 1",
        "id": 13
    }, {
        "name": "Type 3",
        "id": 15
    }],
    [{
        "name": "Type 2",
        "id": 14
    }],
    [{
        "name": "Type 1",
        "id": 13
    }]
]
Run Code Online (Sandbox Code Playgroud)

对此:

["Type 1","Type 2","Type 3"]
Run Code Online (Sandbox Code Playgroud)

即没有重复和"名称"属性.

任何建议非常感谢.

javascript json underscore.js lodash

13
推荐指数
2
解决办法
1万
查看次数

使用Lodash从对象/值中删除键

我试图最终得到2个对象数组,a&b.如果键'name'出现在数组a中,我不希望它出现在b中.

var characters = [
  { 'name': 'barney', 'blocked': 'a', 'employer': 'slate' },
  { 'name': 'fred', 'blocked': 'a', 'employer': 'slate' },
  { 'name': 'pebbles', 'blocked': 'a', 'employer': 'na' },
  { 'name': 'pebbles', 'blocked': 'b', 'employer': 'hanna' },
  { 'name': 'wilma', 'blocked': 'c', 'employer': 'barbera' },
  { 'name': 'bam bam', 'blocked': 'c', 'employer': 'barbera' }
];
var a = _.filter(characters, { 'blocked': 'a' });
var z = _.pluck(a,'name');
var b = _.difference(characters, a);
_(z).forEach(function (n) {

    //_.pull(b, _.filter(b, { 'name': n …
Run Code Online (Sandbox Code Playgroud)

arrays lodash

13
推荐指数
1
解决办法
4万
查看次数

来自lodash下划线或其他库的不可变_.assign(与克隆一起分配)?

是否有一种替代方法可用于lodash,下划线或其他几乎表现相同的库,除了它返回一个新对象而不是改变第一个参数?

var o = { 'user': 'barney' }
var result = method(o, { 'age': 40 }, { 'user': 'fred' })

// o still { 'user': 'barney' }
// result is now { 'user': 'fred', 'age': 40 }
Run Code Online (Sandbox Code Playgroud)

javascript underscore.js lodash

13
推荐指数
3
解决办法
9033
查看次数

lodash _.contains string中的多个值之一

在lodash中有没有办法检查字符串是否包含数组中的一个值?

例如:

var text = 'this is some sample text';
var values = ['sample', 'anything'];

_.contains(text, values); // should be true

var values = ['nope', 'no'];
_.contains(text, values); // should be false
Run Code Online (Sandbox Code Playgroud)

javascript lodash

13
推荐指数
2
解决办法
3万
查看次数