简体   繁体   English

是否有一个loadash函数可以比较两个数组并仅在arr1中存在来自arr2的所有值时才返回true?

[英]Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1?

I have to compare two arrays and return true if only the array 1 contains all values of array 2. What is the suitable loadash function for this? 我必须比较两个数组,如果只有数组1包含数组2的所有值,则返回true。对此合适的loadash函数是什么?

let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2] 
let arr3 = [1, 5] 

comparing arr1 with arr2 should return true comparing arr1 with arr3 should return false 将arr1与arr2比较应返回true将arr1与arr3比较应返回false

Just for completeness, you could take a Set and compare with Set#has . 仅出于完整性考虑,您可以选择Set并与Set#has进行比较。

 let arr1 = [1, 2, 3, 4], arr2 = [1, 2], arr3 = [1, 5], base = new Set(arr1); console.log(arr2.every(Set.prototype.has, base)); console.log(arr3.every(Set.prototype.has, base)); 

You can just use every : 您可以只使用every

 let arr1 = [1, 2, 3, 4]; let arr2 = [1, 2]; let arr3 = [1, 5]; const allElements = (a1, a2) => a2.every(e => a1.includes(e)); console.log(allElements(arr1, arr2)); console.log(allElements(arr1, arr3)); 

There is no need for loadash here just use native JavaScript Array#every method with Array#includes method 这里不需要loadash,只需将本机JavaScript Array#every方法与Array#includes方法一起使用

 function compareArray(arr1, arr2) { return arr2.every(v => arr1.includes(v)) } let arr1 = [1, 2, 3, 4] let arr2 = [1, 2] let arr3 = [1, 5] console.log(compareArray(arr1, arr2)) console.log(compareArray(arr1, arr3)) 

let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2] 

for(let x of arr2) {
    _.includes(arr1, x) || true;
}

You can use _.difference 您可以使用_.difference

 let arr1 = [1, 2, 3, 4] let arr2 = [1, 2] let arr3 = [1, 5] console.log(_.difference(arr2,arr1).length === 0) console.log(_.difference(arr3,arr1).length === 0) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script> 

You can use _.difference in such way: 您可以通过_.difference方式使用_.difference

let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2] 

const b = _.difference(arr2, arr1).length === 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 制作一个接收两个 arrays 的 function。 将 arr1 和 arr2 中的所有数字相加。 如果 arr1 的总和等于 arr2 返回 true。 如果不是,则为假 - make a function that takes in two arrays. Add all the numbers in arr1 & arr2. If the sum of arr1 is equal to arr2 return true. False if not 我正在尝试编写 function 其中 arr1[0] = arr2[0], arr1[1] = arr2[1], ... 等等。 我该怎么做呢? - I am trying to write a function where arr1[0] = arr2[0], arr1[1] = arr2[1], … and so on. How do I do this? 在给定索引 n 处将 arr1 复制到 arr 2,并且在 function 运行后 arr 1 和 arr2 应该相同 - copy arr1 to arr 2 at given index n and arr 1 and arr2 should be same after function runs 如何从arr1和arr2获取结果,当ID匹配时我需要从arr1复制内容 - How can I get the result from arr1 and arr2, When the ID matches I need to copy the content from arr1 arr1.concat(arr2)和[] .concat(arr1,arr2)之间的区别 - Difference Between arr1.concat(arr2) And [].concat(arr1, arr2) 将getIntersect(arr1,arr2)Javascript函数转换为任意数量的参数并在JQuery中 - Convert getIntersect(arr1, arr2) Javascript function to any number of arguments and in JQuery 从两个数组中找到相似的字符串(obj.key),arr[{},{}] arr2[{},{}],然后将checked 属性设置为true - find the similar string(obj.key) from two array, arr[{},{}] arr2[{},{}],then set checked attribute to true 如何连接arr1数组和arr2数组(基于arr2数组结构) - How to join arr1 array and arr2 array (based on arr2 array structure) a[arr1[i]]=true 有什么作用? - What does a[arr1[i]]=true do? 过滤 arr1 &gt; obj1 &gt; arr2 &gt; obj2 中的多个第一个对象 - filter multiple first objects in arr1 > obj1 > arr2 > obj2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM