Home > Mobile >  Creating Nested arrays
Creating Nested arrays

Time:01-16

I have a main array and I want to create other arrays in the main array, as the sub arrays will have data for egg, baby, intraining.

I am not sure if this is the correct way of doing it?

The reason why I want to do it this way,is that, if I want to access the egg.name in the digiListarray, I know it would work without any errors poping up when I do console.log(digiList.egg.name)

Currently the error I get is 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"
 }
]
]

]

CodePudding user response:

What you have is not a valid array. If your goal is being able to access the data via do notation, eg. digiList.egg.name then using arrays is not how to get there. You would need to use objects. Assuming you have one object in each of the data elements, egg, baby and intraining then you would take the following approach:

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

CodePudding user response:

You can't declare a variable inside an array.

But you can declare it outside and then refer to it inside, e.g.

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

console.log(digiList)

// output: [2, 4, 12] 

In your case you might refer to the answer given from Lalit Tyagi, it should fulfill your needs.

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

CodePudding user response:

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

CodePudding user response:

   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;

  • Related