1

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:

  1. the lengths of the arrays are unknown
Alt-01
  • 7
  • 3
  • Hi there. If I understood correctly, then this is the kind of question you are asking: https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – m1ck May 31 '23 at 20:23
  • @m1ck if you're doing `Object.keys({ varName })[0]` to get the name of the variable, you're doing it wrong. You literally just typed out the name of the variable. Why not just write `"varName"` as a string instead? – code May 31 '23 at 20:27
  • @code are you suggesting that I misunderstood the question, and that it doesn't align with the question I posted the link to? – m1ck May 31 '23 at 20:39
  • @m1ck, I'm asking how to do that to the multidimensional array. – Alt-01 May 31 '23 at 21:10
  • @m1ck you may have understood the question, but the other question you linked does not directly answer this one. In fact, the approach you linked would be horrible for this case. – code May 31 '23 at 21:34

1 Answers1

1

It's quite roundabout to directly convert a variable name to a string. What you can do is use an object containing arrays instead of one multidimensional array, and take advantage of using the object property shorthand (see shorter notation):

const fruits = ["apple","Mango","Banana","Pineaple","Pear"];
const vegetables = ["Okra", "Spinach", "Kenef", "Bitter Leaf", "Bell [Peppers"];
const proteins = ["Fish", "Caviar", "Chicken", "Eggs", "Beans"];

const shoppingList = {fruits, vegetables, proteins};

for(const category in shoppingList) {
  console.log(`[${category}]`);
  for(const item of shoppingList[category])
    console.log(" -", item)
  console.log("")
}
code
  • 5,690
  • 4
  • 17
  • 39