javascript 创建嵌套数组

7vhp5slm  于 2023-01-16  发布在  Java
关注(0)|答案(4)|浏览(210)

我有一个主数组,我想在主数组中创建其他数组,因为子数组将包含egg,baby,intraining的数据。
我不确定这是否是正确的做法?
我之所以要这样做,是因为如果我想访问digiList数组中的egg . name,我知道在执行console.log(digiList.egg.name)时它会正常工作,不会弹出任何错误
目前我得到的错误是Egg is not defined

const digiList = [
 egg = [           
   {
     id:"blue",
     eggtype:"blue",
     digivolution:"Punimon",
     image:"../Images/Blue Egg - mobile.png"
    },        
],    

baby =[
 {
   id:"botamon",
   name:"Botamon",
   stage:"Baby",
   type:"Data",
   preDigivolution:["green"],
   digivolution:["Koromon"],
   image: "https://www.grindosaur.com/img/games/digimon-world/digimon/12-botamon.jpg"
  },

intraining = [
 {
  id:"koromon",
  name:"Koromon",
  stage:"In-training",
  type:"Data",
  preDigivolution:["Botamon"],
  digivolution:["Agumon","Gabumon","Kunemon"],
  image: "https://www.grindosaur.com/img/games/digimon-world/digimon/50-koromon.jpg"
 }
]
]

]
hiz5n14c

hiz5n14c1#

你所拥有的不是一个有效的数组。如果你的目标是通过do符号来访问数据,例如digiList.egg.name,那么使用数组并不是实现这一目标的方法。你需要使用对象。假设你在每个数据元素eggbabyintraining中有一个对象,那么你将采取以下方法:

const 
    egg = { id:"blue", name:"somename otherwise 'undefined'", eggtype:"blue", digivolution:"Punimon", image:"../Images/Blue Egg - mobile.png" },
    baby = { id:"botamon", name:"Botamon", stage:"Baby", type:"Data", preDigivolution:["green"], digivolution:["Koromon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/12-botamon.jpg" },
    intraining = { id:"koromon", name:"Koromon", stage:"In-training", type:"Data", preDigivolution:["Botamon"], digivolution:["Agumon","Gabumon","Kunemon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/50-koromon.jpg" },
    
    digiList = { egg, baby, intraining };
    
    console.log( digiList.egg.name, digiList.baby.name, digiList.intraining.name );
8nuwlpux

8nuwlpux2#

不能在数组中声明变量。
但是你可以在外部声明它,然后在内部引用它。

let egg = 1
const digiList =  [
  egg = 2, egg=4, egg=egg*3
]

console.log(digiList)

// output: [2, 4, 12]

在你的情况下,你可以参考Lalit Tyagi给出的答案,它应该能满足你的需要。

const egg = { id:"blue", name:"somename otherwise 'undefined'", eggtype:"blue", digivolution:"Punimon", image:"../Images/Blue Egg - mobile.png" };
const baby = { id:"botamon", name:"Botamon", stage:"Baby", type:"Data", preDigivolution:["green"], digivolution:["Koromon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/12-botamon.jpg" };
const intraining = { id:"koromon", name:"Koromon", stage:"In-training", type:"Data", preDigivolution:["Botamon"], digivolution:["Agumon","Gabumon","Kunemon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/50-koromon.jpg" };
    
const digiList = { egg, baby, intraining };
    
console.log( digiList.egg.name );
4uqofj5v

4uqofj5v3#

const egg = { id:"blue", name:"somename otherwise 'undefined'", eggtype:"blue", digivolution:"Punimon", image:"../Images/Blue Egg - mobile.png" };
const baby = { id:"botamon", name:"Botamon", stage:"Baby", type:"Data", preDigivolution:["green"], digivolution:["Koromon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/12-botamon.jpg" };
const intraining = { id:"koromon", name:"Koromon", stage:"In-training", type:"Data", preDigivolution:["Botamon"], digivolution:["Agumon","Gabumon","Kunemon"], image: "https://www.grindosaur.com/img/games/digimon-world/digimon/50-koromon.jpg" };
    
const digiList = { egg, baby, intraining };
    
console.log( digiList.egg.name );
2g32fytz

2g32fytz4#

const digiList = [
 [           
   {
     id:"blue",
     eggtype:"blue",
     digivolution:"Punimon",
     image:"../Images/Blue Egg - mobile.png"
    },        
],    

[
 {
   id:"botamon",
   name:"Botamon",
   stage:"Baby",
   type:"Data",
   preDigivolution:["green"],
   digivolution:["Koromon"],
   image: "https://www.grindosaur.com/img/games/digimon-world/digimon/12-botamon.jpg"
  },

 [
 {
  id:"koromon",
  name:"Koromon",
  stage:"In-training",
  type:"Data",
  preDigivolution:["Botamon"],
  digivolution:["Agumon","Gabumon","Kunemon"],
  image: "https://www.grindosaur.com/img/games/digimon-world/digimon/50-koromon.jpg"
 }
]
]
]

const[egg, baby] = digiList;

const[,intraining] = baby;

相关问题