从对象数组中删除对象 [英] Remove object from an array of objects

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

问题描述

我的问题应该很简单,但是我没有.

my problem should be rather simple, but i didn't got it.

例如,我从数据库中获取了以下数据:

For example i have the following data from the database:

@user = User.all

然后我有另一个用户数组

then i have an other array of user

other_user = getting_them_from_somewhere_else

现在,我遍历两个数组,并通过检查电子邮件来检查某些用户是否仍在数据库中:

Now i iterate over the two arrays and check if some users are still in the database by checking for the emails:

@other_user.each do |o|
    @user.each do |u|
        if o["email"] == u["user_mail"]
            @user.delete(u)
            break
        end
    end

    ... do something with o ...
end

方法@ user.delete(u)从数据库中删除用户,但是我只想从数组@user中删除对象u.

The method @user.delete(u) deletes the user from the database, but i just want to remove the object u from the array @user.

推荐答案

我认为您不需要使用@ user.email,只需:

I think you don't need to use @user.email, just:

  @users.delete_if {|user| @other_user.include? user}

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

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