I would use an object to contains the various types:
var type = {
fruit: ["apple", "banana", "orange"],
veggie: ["carrot", "pea", "corn"]
}
If you want to find the name of the array the thing is in loop over the arrays in the object, using indexOf to test for the thing's presence.
function findThing(thing) {
for (var k in type) {
if (type[k].indexOf(thing) > -1) return k;
}
return 'unknown';
}
console.log('An apple is a ' + findThing('apple')); // An apple is a fruit
console.log('An froom is a ' + findThing('froom')); // An froom is a unknown
Fiddle
On the other hand, if you wanted to return some more information (like the type, size, and other items, you could do this:
function returnArrayOfThing(thing) {
for (var k in type) {
if (type[k].indexOf(thing) > -1) return k + ' which contains ' + type[k].length + ' items: ' + type[k];
}
return 'unknown';
}
console.log('An apple is in ' + returnArrayOfThing('apple')); // An apple is in fruit which contains 3 items: apple,banana,orange
fruitcontains "apple" andveggiedoes not. Assuming you cannot write that function yourself, which is a fair assumption given your question ^_^fruit.contains("apple"), but I'm trying to get it so I don't need to knowvar fruit.