英文:
How can I display the variable names of 1D arrays used in a multidimensional array in JavaScript?
问题
以下是翻译好的部分:
我现在正在学习JavaScript,所以这个问题是为了满足我的好奇心。
有3个一维数组如下:
let fruits = ["苹果", "芒果", "香蕉", "菠萝", "梨"];
let vegetables = ["秋葵", "菠菜", "开心果", "苦叶", "彩椒"];
let proteins = ["鱼", "鱼子酱", "鸡肉", "鸡蛋", "豆类"];
这三个数组已用于创建一个多维数组,如下所示:
let shoppingList = [fruits, vegetables, proteins];
我想要以这样的方式循环遍历"shoppingList",在显示项目之前显示1D数组的名称,如下所示:
水果
苹果
芒果
香蕉
菠萝
梨
蔬菜
秋葵
菠菜
开心果
苦叶
彩椒
...等等
目前我无法做到这一点,因为只能显示1D数组中的项目。
我已经如下循环遍历它们,没有问题:
for(let listCategory of shoppingList){
for(let listItem of listCategory){
console.log(listItem);
}
}
这给我以下结果:
苹果
芒果
香蕉
菠萝
梨
秋葵
菠菜
开心果
苦叶
彩椒
鱼
鱼子酱
鸡肉
鸡蛋
豆类
但我想要的是这个:
水果(变量名称)
苹果
芒果
香蕉
菠萝
梨
蔬菜(变量名称)
秋葵
菠菜
开心果
苦叶
彩椒
蛋白质(变量名称)
鱼
鱼子酱
鸡肉
鸡蛋
豆类
在键入所有这些后,我只是想知道这是否可能?
假设:
- 数组的长度未知。
英文:
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
答案1
得分: 1
直接将变量名转换为字符串相对繁琐。您可以使用包含数组的对象而不是一个多维数组,并利用对象属性的简写方式(参见更短的表示法):
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("")
}
以上为代码部分,已翻译完毕。
英文:
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):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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("")
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论