Determining whether one array contains the contents of another array in JavaScript/CoffeeScript

后端 未结 5 733
孤独总比滥情好
孤独总比滥情好 2020-12-02 18:20

In JavaScript, how do I test that one array has the elements of another array?

arr1 = [1, 2, 3, 4, 5]
[8, 1, 10, 2, 3, 4, 5, 9].function_name(arr1) # => t         


        
5条回答
  •  渐次进展
    2020-12-02 19:09

    ES6 solution using includes:

    [1].every(elem => [1,2,3].includes(elem));
    

    Very similar to Explosion Pills's solution above, just a bit more readable (and arguably a tiny, tiny bit slower).

提交回复
热议问题