-1

If array A contains all weekdays in Array B, then I want to return True.

var A = ["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"]; var B = ["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"];

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Do you mean "if all days of the week in Array A are contained in array B"? –  Jun 26 '16 at 08:21
  • Also see http://stackoverflow.com/questions/15514907/determining-whether-one-array-contains-the-contents-of-another-array-in-javascri. This came up in the top few results for the google search for "javascript array all values contained other array". Also related: http://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array. –  Jun 26 '16 at 08:26

1 Answers1

0

You can do like

B.every(e => A.some(f => e == f))
Redu
  • 25,060
  • 6
  • 56
  • 76
  • Although the OP's description is a bit garbled, I think you have this backward. But in any case why wouldn't you use `indexOf` or `includes`? –  Jun 26 '16 at 08:23
  • @torazaburo yes at first i thought so but then reading it over the OP says array A contains all elements in B so it's checking B over A to see if B is a subset of A. Seeing all weekdays in B is a little deceiving. Yes `indexOf` is much faster than `some` for some reason. Yes probably includes would be the best though. Another approach could be to take intersection and compare lengths. – Redu Jun 26 '16 at 08:43
  • I'm pretty sure he meant "If array A contains **only** weekdays [which are contained in] in Array B", in other words, "if all the weekdays contained in array A are in Array B". –  Jun 26 '16 at 09:45