1

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

3
  • Can you be more specific what you are asking ! Commented Mar 3, 2014 at 14:36
  • 1
    Google for 'JavaScript Array contains value function', then use that function to determine fruit contains "apple" and veggie does not. Assuming you cannot write that function yourself, which is a fair assumption given your question ^_^ Commented Mar 3, 2014 at 14:37
  • I can use something like fruit.contains("apple"), but I'm trying to get it so I don't need to know var fruit. Commented Mar 3, 2014 at 14:45

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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;
}

Comments

0

Use the object to contain these two arrays like this.

JSFIDDLE

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];
        }
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.