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

javascript compare two arrays of objects for matches

javascript compare two arrays for matches

javascript check if two arrays have same elements

comparing two arrays in javascript returning differences

javascript compare two arrays of objects and remove duplicates

lodash compare arrays

javascript check if two arrays have common elements

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

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));

**How to find if two arrays contain any common item in Javascript ,** Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1? - javascript. Here, we use both the Python Numpy <= operator and Python less_equal function to compare two and three-dimensional arrays. The below code returns true if a value in arr1 is less than or equal to arr2 otherwise, False.

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));

**Comparison of Two arrays Using JavaScript,** Compare each and every item from array1 to array2 and if it finds any common item then return true otherwise return false. Declare two array If element exist then return true else return false. function findCommonElements2(arr1, arr2) { How to get values from html input array using JavaScript ? Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.

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))

**Chapter 18. Arrays,** In above code first, we are chaining arr1 with forEach() method gives us back each element in the arr1 at the end we are returning the second� This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

let arr1 = [1, 2, 3, 4] let arr2 = [1, 2] for(let x of arr2) { _.includes(arr1, x) || true; }

**How to check if two arrays are equal with JavaScript?,** The values (the range of the map) are called the array's elements. The array method push() provides another way of appending an element: > var arr = [ 'a', ' b' ]; > arr.length 2 > delete arr[1] // does not update length true > arr [ 'a', ] > arr. length Instead, it returns an empty array with length 2 (all it contains are two holes). I've been given a homework assignment to make a function to check whether 2 given arrays with the same given size have exactly the same set of elements. My function seems to be working but I feel

You can use `_.difference`

in such way:

let arr1 = [1, 2, 3, 4] let arr2 = [1, 2] const b = _.difference(arr2, arr1).length === 0

**Array Intersection, Difference, and Union with JavaScript ES7,** Array. prototype. diff = function(arr2) { var ret = []; for(var i in this) { if(arr2. declare the function with two parameters. function compare(arr1,arr2){} Step How to check all values of an array are equal or not in JavaScript , With JavaScript, the best Comparing Arrays of Objects in JavaScript, This will return the difference� Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though. Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.

**Arrays,** Likewise, you may need to find the union and intersection between two arrays as well. if (a2.indexOf(a1[i]) === -1) { result.push(a1[i]); } } return result; }. Compare this to In this case, you'll get an array containing all the elements of arr1 that are not in arr2 , and Because Sets in JavaScript contain only distinct elements. The function returns true only if the two arrays contain the same number of values and each value in one array has an exact duplicate in the other array. Everything else will return false. my alternative method for evaluating if two arrays contain (all) identical values:

**Documentation,** There are two syntaxes for creating an empty array: The total count of the elements in the array is its length : mix of values let arr = [ 'Apple' , { name : ' John' } , true , function Extracts the last element of the array and returns it: The loop for..in iterates over all properties, not only the numeric ones. The arrays are of the same size. Example: arr1= [1, 2, 1, 3, 2, 1] arr2= [2, 2, 3, 1, 1, 1] Solution: There are several ways to solving this problem and one is by sorting both of the array. Then we can check elements one by one and if the two arrays are similar, it has to match for every single element. So, after sorting, a[i] must be b[i] for

**JavaScript Arrays - tips, tricks and examples,** Creates an array of array values not included in the other given arrays using SameValueZero This method is like _.difference except that it accepts iteratee which is invoked for each for equality comparisons, in which only the first occurrence of each element is kept. (boolean): Returns true if value is found, else false . Given two sorted arrays. There is only 1 difference between the arrays. The first array has one element extra added in between. Find the index of the extra element. Examples: Input: {2, 4, 6, 8, 9, 10, 12}; {2, 4, 6, 8, 10, 12}; Output: 4 Explanation: The first array has an extra element 9. The extra element is present at index 4.

##### Comments

- Why not use the pure js?
`arr2.every(i => arr1.includes(i));`

... ? Is there any reason for usign lodash here?- Possible duplicate of Check if an array is subset of another array and Check if array contains all elements of another array
- Since you have chosen a non-lodash answer, mention of "lodash" in the title and tags seems to be irrelevant and misleading now.
- Sorry i found a solution in the comments section, but there were no direct answer! What should i do now?
- 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, why should i use a function of a frame work, if i have a function which is native implemented in the language?
- 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 You are right. I misunderstood. I have not updated the answer
- How does your answer differ from mine?