如何使用Lodash从数组中删除对象? [英] How can I remove object from array, with Lodash?

查看:750
本文介绍了如何使用Lodash从数组中删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Lodash从数组中删除对象.

I'm trying to remove an object from an array using Lodash.

server.js 中(使用NodeJS):

In server.js (using NodeJS):

    var lodash = require('lodash')();

    var rooms = [
      { channel: 'room-a', name: 'test' },
      { channel: 'room-b', name: 'test' } 
    ]

我尝试了两个命令,但没有用:

I tried with two commands and it did not work:

    var result = lodash.find(rooms, {channel: 'room-a', name:'test'});
    var result = lodash.pull(rooms, lodash.find(rooms, {channel: 'room-a', name:'test'}));

这是 console.log(result)的输出:

    LodashWrapper {
      __wrapped__: undefined,
      __actions__: [ { func: [Function], args: [Object], thisArg: [Object] } ],
      __chain__: false,
      __index__: 0,
      __values__: undefined }

有人可以帮助我吗?谢谢!

Can someone help me? Thank you!

推荐答案

require('lodash')()

调用 lodash 函数(通过())创建一个LoDash对象,该对象包装了 undefined .

Calling the lodash function (by ()) creates a LoDash object that wraps undefined.

这不是您想要的;您需要包含静态方法的 lodash 函数本身.

That's not what you want; you want the lodash function itself, which contains static methods.

删除.

这篇关于如何使用Lodash从数组中删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆