Getting name from nesting arrays from matching arrays

Viewed 62

I have some arrays, and if they contain similar values, I would like to return the name of those arrays.


    var x = {
      "food": ['bacon', 'cheese', 'bread', 'tomato'],
      "utilities": ['plates', 'forks', 'spatulas'],
      "guests": ['john', 'matt', 'bill']
    },
    y = ['bacon', 'tomato', 'forks'];

I have my variable x, and it has multiple arrays, with a name either food, or utilities, or guests. All that y contains, is some values that are the same within some of those arrays in the variable of x. I need to return the name of the arrays that contain bacon,tomato, and forks in their arrays. So for this example, I need to return: ["food", "utilities"].


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

When throwing them through this function, I get an error *********. How may I go and get a returned array of ["food", "utilities"]?

3 Answers
Related