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];
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?
To check if a JavaScript array contains a certain value or element, you can use the includes () method of the Array object. The includes () method returns the Boolean true if your array contains the value you specified as its argument. Otherwise, the method returns false.
The array includes () is a built-in JavaScript method that defines whether the array contains the specified element or not. The includes () function accepts element and start parameters and returns true or false as output depending on the result. The includes () method is case sensitive.
Learn how to use the includes () Array method to search for a certain value in JavaScript To check if a JavaScript array contains a certain value or element, you can use the includes () method of the Array object. The includes () method returns the Boolean true if your array contains the value you specified as its argument.
Syntax of JavaScript Array Contain are given below: The above syntax of includes () method is explained in detail below: sampleArray: It can be an array variable containing any number of elements in which you want to determine whether it contains the particular value or not.
var master = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3];
var sub = [777, 22, 22];
console.log(master.join(',').includes(sub.join(',')))
//true
You can do this by simple console.log(master.join(',').includes(sub.join(',')))
this line of code using include method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With