Reputation: 125
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 digiList
array, 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"
}
]
]
]
Upvotes: 0
Views: 88
Reputation:
after I read the comments, it was right to say something about what I was doing. here is the thing, based on the es6 and beyond array may contain an array, literals, objects, and another array and inside the array, it is not advisable to declare a variable. so in the end, you know array destructuring and object destructuring to use them. by the way, I appetite for your constructive comments.
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;
Upvotes: 0
Reputation: 274
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 );
Upvotes: 1
Reputation: 625
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 );
Upvotes: 1
Reputation: 24638
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 );
Upvotes: 1