0

I need a way to get the title/name of an array, just by detecting the values inside of that array in Javascript.

I have an json array where there are the arrays of greeting and food. The variable x is equal to hi and the variable y is equal to pizza.

    var $arrays = {greeting: ['hello', 'hi'], food: ['pizza', 'cookie']};
    var x = 'hi',
        y = 'pizza';

I need to now return the name of greeting for x, and the name of food for y.

    if(x=='hi') {
        alert($array.greeting); // alert('greeting');
    }
    if(y=='pizza') {
        alert($array.food); // alert('food');
    }

In which way would this work better, in Javascript or jQuery, how so?

2
  • in javascript one = is assignation == or ==== is condition evaluation Commented Jul 12, 2017 at 22:42
  • 1
    This has nothing to do with jQuery. Also, why are you creating your objects in quotes ? Commented Jul 12, 2017 at 22:46

1 Answer 1

1

Depending on the size/structure of your object containing the arrays, you can simply iterate over they keys and find the key whose value (array) contains the value you are looking for. Here's how it would look.

var $arrays = {
  "greeting": ['hello', 'hi'],
  "food": ['pizza', 'cookie']
};
var x = 'hi',
    y = 'pizza';

function getContainerName(value) {
  return Object.keys($arrays).find(function(key) {
    return $arrays[key].find(function(elem) {
      return elem === value;
    })
  });
}

console.log(getContainerName(x));
console.log(getContainerName(y));

Alternatively in ES6 syntax

return Object.keys($arrays).find(key => $arrays[key].find(elem => elem === value));


UPDATE - To make this search for an array of values and find the name of the property that contains all values in the array, you can use the .every() function in conjunction with the above code, like so:

y = ['chips', 'pizza'], x = {"food":['banana', 'carrot', 'pizza', 'wheat', 'chips']};

function getContainerName(obj, values) {
  return Object.keys(obj).find(function(key) {
    return values.every(value => obj[key].find(function(elem) {
      return elem === value;
    }));
  });
}

console.log(getContainerName(x, y));

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

5 Comments

A little late, would this work if x was an array where it needs to find a specific value in that array? eg. y = ['chips', 'pizza'], x = {"food":['banana', 'carrot', 'pizza', 'wheat', 'chips']} So, it would need to find chips and pizza, in the x array, so console.log(getContainerName(x)) would return food?
@Ackados Updated. Take a look
Awesome, last thing, I tried modifying it for there to be more than just food in the array, and added greetings, but I get undefined error when I change pizza in y to hi. jsfiddle.net/u1zLm71f
@Ackados Well, that changes the problem more than you might think. You should open up a new question and I can answer it there.

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.