I am now learning javascript so this question is to satisfy my curiousity.
there are 3 1D arrays as follows:
let fruits = ["apple","Mango","Banana","Pineaple","Pear"];
let vegetables = ["Okra", "Spinach", "Kenef", "Bitter Leaf", "Bell [Peppers"];
let proteins = ["Fish", "Caviar", "Chicken", "Eggs", "Beans"];
all three arrays have been used to create a multidimensional array as follows:
let shoppingList = [fruits, vegetables, proteins];
I want to loop through "shoppingList" in such a way that the name of the 1d array is displayed before the items are shown, like this:
fruits
Apple Mango Banana Pineaple Pear
Vegetables
Okra Spinach Kenef Pineaple Pear
... and so on
I am currently unable to do this, as only the items in the 1d arrays can be displayed.
I have looped through them as follows with no problem:
for(let listCategory of shoppingList){
for(let listItem of listCategory){
console.log(listItem);
}
}
and that gives me the following: apple Mango Banana Pineaple Pear Okra Spinach Kenef Bitter Leaf Bell [Peppers Fish Caviar Chicken Eggs Beans
But what i want is this: fruits (variable name) apple Mango Banana Pineaple Pear
vegetables (variable name) Okra Spinach Kenef Bitter Leaf Bell [Peppers
proteins (variable name) Fish Caviar Chicken Eggs Beans
after typing all this Im just wondering even if that is possible?
assumptions:
- the lengths of the arrays are unknown