检查是否可以在另一个数组中找到所有项目 [英] Check if all items can be found in another array
本文介绍了检查是否可以在另一个数组中找到所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查数组中的所有项目是否可以在另一个数组中找到.也就是说,我需要检查一个数组是否是另一个数组的子集.
I need to check if all items in an array can be found within another array. That is, I need to check if one array is a subset of another array.
示例:
var array = [1, 2, 5, 7];
var otherArray = [1, 2, 3, 4, 5, 6, 7, 8];
比较上面的两个数组应该返回true,因为 array
中的所有项目都可以在 otherArray
中找到.
Comparing these two arrays above should return true as all items in array
can be found in otherArray
.
var array = [1, 2, 7, 9];
var otherArray = [1, 2, 3, 4, 5, 6, 7, 8];
比较上面的两个数组应该返回false,因为在 otherArray
中找不到 array
中的一项.
Comparing these two arrays above should return false as one of the items in array
cannot be found in otherArray
.
我尝试在for循环内使用 indexOf
方法,但未成功.我希望有人能帮助我.:)
I have tried to use the indexOf
method inside a for loop without success.
I hope someone could help me. :)
推荐答案
查看全文