如何在JavaScript中显示多维数组中使用的1D数组的变量名称?

huangapple go评论60阅读模式
英文:

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

这给我以下结果:
苹果
芒果
香蕉
菠萝

秋葵
菠菜
开心果
苦叶
彩椒

鱼子酱
鸡肉
鸡蛋
豆类

但我想要的是这个:
水果(变量名称)
苹果
芒果
香蕉
菠萝

蔬菜(变量名称)
秋葵
菠菜
开心果
苦叶
彩椒

蛋白质(变量名称)

鱼子酱
鸡肉
鸡蛋
豆类

在键入所有这些后,我只是想知道这是否可能?

假设:

  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

答案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 = [&quot;apple&quot;,&quot;Mango&quot;,&quot;Banana&quot;,&quot;Pineaple&quot;,&quot;Pear&quot;];
const vegetables = [&quot;Okra&quot;, &quot;Spinach&quot;, &quot;Kenef&quot;, &quot;Bitter Leaf&quot;, &quot;Bell [Peppers&quot;];
const proteins = [&quot;Fish&quot;, &quot;Caviar&quot;, &quot;Chicken&quot;, &quot;Eggs&quot;, &quot;Beans&quot;];

const shoppingList = {fruits, vegetables, proteins};

for(const category in shoppingList) {
  console.log(`[${category}]`);
  for(const item of shoppingList[category])
    console.log(&quot; -&quot;, item)
  console.log(&quot;&quot;)
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 04:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定