How do I test if one array is a subset of another?

What's the best (cleanest) way to provide this sort of logic?

var colors = ["red","white","blue"];

logic(colors,["red","green"]); //false
logic(colors,["red"]); //true
logic(colors,["red","purple"]); //false
logic(colors,["red","white"]); //true
logic(colors,["red","white","blue"]); //true
logic(colors,["red","white","blue","green"]); //false
logic(colors,["orange"]); //false

Possibly using underscore.js?

Answers


Assuming each element in the array is unique: Compare the length of hand with the length of the intersection of both arrays. If they are the same, all elements in hand are also in colors.

var result = (hand.length === _.intersection(hand, colors).length);

DEMO


Maybe difference is what you are looking for:

_(hand).difference(colors).length === 0

Need Your Help

Tool / utility for checking debugging XPath queries

windows xpath

Has anyone got any recommendations for tools/utilities to use in creating/debugging XPath queries for Windows ?

Android Shared preferences with multiple activities

java android android-activity sharedpreferences

How do I retrieve shared preferences that have been saved from a