确定一个数组是否包含在JavaScript中/ CoffeeScript的另一个数组的内容 [英] Determining whether one array contains the contents of another array in JavaScript/CoffeeScript

查看:133
本文介绍了确定一个数组是否包含在JavaScript中/ CoffeeScript的另一个数组的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,如何测试一个数组另一个数组中的元素?

  ARR1 = [1,2,3,4,5]
[8,1,10,2,3,4,5,9] .function_name(ARR1)#=>真正


解决方案

没有设置功能做到这一点,但你可以简单地做一个特设的阵列路口和检查长度。

  [8,1,10,2,3,4,5,9] .filter(功能(ELEM){
    返回arr1.indexOf(ELEM)GT; -1;
})。长度== arr1.length

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) # => true

解决方案

No set function does this, but you can simply do an ad-hoc array intersection and check the length.

[8, 1, 10, 2, 3, 4, 5, 9].filter(function (elem) {
    return arr1.indexOf(elem) > -1;
}).length == arr1.length

这篇关于确定一个数组是否包含在JavaScript中/ CoffeeScript的另一个数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆