Reputation: 23
is it possible to get an array if you only know one of its values?
I've got two arrays and want to find which one has the variable:
var fruit = ["apple","banana","orange"];
var veggie = ["carrot","pea","corn"];
alert("An apple is a " + /*return array with value "apple"*/);
Is it possible to do this with Javascript? Thanks
Upvotes: 1
Views: 58
Reputation: 63587
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
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
Upvotes: 0
Reputation: 6351
Use the object to contain these two arrays like this.
var food = {
fruit : ["apple","banana","orange"],
veggie : ["carrot","pea","corn"]
}
var arr = getFood('apple', food);
//returns ["apple","banana","orange"];
var arr2 = getFood('pea', food);
//returns ["carrot","pea","corn"];
function getFood(item, food){
for (var key in food){
if(food[key].indexOf(item) != -1){
return food[key];
}
}
}
Upvotes: 0
Reputation: 27853
If you only have the two categories, then you can do a simple if check:
var fruit = ["apple","banana","orange"];
var veggie = ["carrot","pea","corn"];
var typeOfApple = 'unknown';
if (fruit.indexOf('apple') !== -1) {
typeOfApple = 'fruit';
} else if (veggie.indexOf('apple') !== -1) {
typeOfApple = 'veggie';
}
alert("An apple is a " + typeOfApple);
If you have more categories (or simply want your code to be more robust), you could use an object with keys as categories and values arrays with items from that category:
var types = {
fruit : ["apple","banana","orange"],
veggie : ["carrot","pea","corn"]
}
Then you can check every category if it contains your item:
var typeOfApple = 'unknown';
for (var type in types) {
if (types[type].indexOf('apple') !== -1)
typeOfApple = type;
}
alert("An apple is a " + typeOfApple);
Upvotes: 0
Reputation: 4638
You can use a function like below, and call it on each concerned array:
function contains(a, obj) { //a is the array to check, and obj the object you are looking for
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
Upvotes: 0
Reputation: 958
Wrap the alert in an if/else statement and check the index of 'apple'. If fruit.indexOf('apple') != -1
then it must be a fruit.
Upvotes: 1