-1

If i have two arrays like so:

var array1 = ['this', 'is', 'array', '1']
var array2 = ['this', 'is', '1']

Is there a built in function that can determine if array1 contains array2. Which in this case it does.

Alternatively some intersect function or similar?

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • 2
    See http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript for discussion re: array intersect – secretformula May 21 '14 at 20:01
  • Do you want an efficient solution, or a short (easy to read) one? And will they always contain strings? – Ry- May 21 '14 at 20:02
  • Short and easy to read is fine as its dealing with very small arrays. They will always be strings yeah. – Marty Wallace May 21 '14 at 20:04

1 Answers1

0

try this with underscore (library based on javascript) http://underscorejs.org/

_.every(array1, function(e){ return _.include(array2, e)}); //=> false
_.every(array2, function(e){ return _.include(array1, e)}); //=> true
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171