0

Short problem: How can I return an array instead of a string.

<html>
  <div class="dinner">Dinner</div>
  <div class="breakfast">BreakFast</div>
</html>

<script>
    var food ={
        breakfast:['eggs', 'bacon', 'oats'],
        dinner:['pizza','wings','tacos']
    }

    $('.dinner').on("click", function(){
            var myArray = ( "food." + $(this).attr('class'))

            console.log(myArray)//this outputs the string food.dinner

            console.log(food.dinner)//this outputs the array ["pizza", "wings", "tacos"]
});
</script>

I'm trying to get console.log(myArray)to return an array.
Problem is when I use .attr to return an elements class name, with the goal of concatenating it with a key to return an array, to use in a function it returns a string instead of the corresponding array, I coded a simplified example of my problem above. Thanks.

George
  • 1
  • 2

0 Answers0