从基于另一个数组的数组中删除对象 [英] Removing objects from an array based on another array

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

问题描述

我有两个这样的数组:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]

如您所见,JamesSteve匹配,我希望能够将它们从arrayA中删除.我怎么写这个?

As you can see, James and Steve match and I want to be able to remove them from arrayA. How would I write this?

推荐答案

像这样:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = find(arrayA, word) {
        arrayA.removeAtIndex(ix)
    }
}
// now arrayA is ["Mike", "Stacey"]

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

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