0

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?

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

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Ahmed
  • 71
  • 1
  • 8
  • Why not use the pure js? – Maheer Ali Apr 08 '19 at 09:11
  • `arr2.every(i => arr1.includes(i));`... ? Is there any reason for usign lodash here? – briosheje Apr 08 '19 at 09:13
  • Possible duplicate of [Check if an array is subset of another array](https://stackoverflow.com/questions/38811421/check-if-an-array-is-subset-of-another-array) and [Check if array contains all elements of another array](https://stackoverflow.com/questions/53606337) – adiga Apr 08 '19 at 09:15
  • Since you have chosen a non-lodash answer, mention of "lodash" in the title and tags seems to be irrelevant and misleading now. – hindmost Apr 08 '19 at 21:00
  • Sorry i found a solution in the comments section, but there were no direct answer! What should i do now? – Ahmed Apr 09 '19 at 12:37

6 Answers6

1

Just for completeness, you could take a Set and compare with 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));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • For further completeness, lodash has an `every` prototype as well, so to "completely" answer the question, though irrelevant, you may want to use `_.every`. – briosheje Apr 08 '19 at 09:17
  • 1
    @briosheje, why should i use a function of a frame work, if i have a function which is native implemented in the language? – Nina Scholz Apr 08 '19 at 09:18
  • I do agree with you and I wouldn't use that either, however, the question is **Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1?** so, it's worth mentioning that lodash has this prototype as well, though I personally would **never** use it. – briosheje Apr 08 '19 at 09:19
0

You can just use 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));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

There is no need for loadash here just use native JavaScript Array#every method with Array#includes method

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))
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2] 

for(let x of arr2) {
    _.includes(arr1, x) || true;
}
Szalbik
  • 133
  • 2
  • 10
0

You can use _.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>
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You can use _.difference in such way:

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

const b = _.difference(arr2, arr1).length === 0
hindmost
  • 7,125
  • 3
  • 27
  • 39