Javascript数组包含/包含子数组 [英] Javascript array contains/includes sub array
本文介绍了Javascript数组包含/包含子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查一个数组是否包含另一个数组。子阵列的顺序很重要,但实际的偏移并不重要。它看起来像这样:
I need to check if an array contains another array. The order of the subarray is important but the actual offset it not important. It looks something like this:
var master = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3];
var sub = [777, 22, 22];
所以我想知道 master
是否包含 sub
类似于:
So I want to know if master
contains sub
something like:
if(master.arrayContains(sub) > -1){
//Do awesome stuff
}
那怎么样这可以用优雅/有效的方式完成吗?
So how can this be done in an elegant/efficient way?
推荐答案
想出了快速思考,但效率取决于大小数组
Just came up with quick thought , but efficiency depends on size of the array
var master = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3];
var sub = [777, 22, 22];
if ((master.toString()).indexOf(sub.toString()) > -1 ){
//body here
}
这篇关于Javascript数组包含/包含子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文