I don't know about the existing, clean, solution, but here is a simple implementation that matches your example:
var match = function(target) {
for (i = 1; i < arguments.length; i += 2) {
if (target[0] == arguments[i][0] && target[1] == arguments[i][1]) {
return arguments[i + 1];
}
}
}
Usage example:
var result = match(["Yellow", "Food"],
["Red", "Food"], "Apple",
["Green", "Plant"], "Grass",
["Yellow", "Thing"], "Schoolbus",
["Yellow", "Food"], "Banana",
["Yellow", "Foot"], "Bad"
)
alert(result) # displays "Banana"
In JavaScript, you cannot compare arrays directly ( [1, 2] != [1, 2]), so you need to compare elements separately.
source
share