Reputation: 2070
Is it possible to check if an array
A=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ"
]
Exists in another array
B=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ",
"CD_WKC",
"DT_INI_WKC"
]
I want to check if all entries in array A exists in B
Upvotes: 26
Views: 31192
Reputation: 2065
You don't need lodash in this case. Here's one liner with vanilla JS.
A.every(i => B.includes(i))
Upvotes: 5
Reputation: 24241
You can use the intersection of the 2 arrays, and then compare to the original.
var A=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ"
];
var B=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ",
"CD_WKC",
"DT_INI_WKC"
];
console.log(_.isEqual(_.intersection(B,A), A));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.2/lodash.js"></script>
Upvotes: 10
Reputation: 4453
var A=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ"
];
var B=[
"EMPRESA",
"CD_MAQ",
"DT_INI_MAQ",
"CD_WKC",
"DT_INI_WKC"
];
if ( _.difference(A,B).length === 0){
// all A entries are into B
}
<script src="https://cdn.jsdelivr.net/lodash/4.16.2/lodash.min.js"></script>
Just use _.difference
Upvotes: 47