I created an item
constructor along with most of the objects I'd like to create for this project I've been messing with. I then stored all the created items(potions,food,weapons,armor) into a multidimensional array.
The problem I'm having now is display these items in the console.
After the items
array is created, I've been using item[subarr1][subarr2].push(item1,item2,item3,item4);
to push the items into their correct arrays, but how do I call properties of the stored objects? I tried, console.log(items[0][0]);
and console.log(items[0][0].name);
but the first one returns [item]
and the other returns undefined
.
here's the code:
//item constructor - defines items parameters
var item = function(name,type,lvl,affect)
{
this.name = name;
this.type = type;
this.lvl = lvl;
this.affect = affect;
}
//create items to be stored in the items Array.
////POTIONS
//health
var hP_10 = new item("Health +10","potion",1,"Heals player by +10.");
var hP_25 = new item("Health +25","potion",1,"Heals player by +25.");
var hP_50 = new item("Health +50","potion",1,"Heals player by +50.");
var hP_100 = new item("Health +100","potion",1,"Heals player by +100.");
//mana
var mP_10 = new item("Mana +10","potion",1,"Refills Mana by +10.");
var mP_25 = new item("Mana +25","potion",1,"Refills Mana by +25.");
var mP_50 = new item("Mana +50","potion",1,"Refills Mana by +50.");
var mP_100 = new item("Mana +100","potion",1,"Refills Mana by +100.");
//energy
var eP_10 = new item("Energy +10","potion",1,"Refills Energy by +10.");
var eP_25 = new item("Energy +25","potion",1,"Refills Energy by +25.");
var eP_50 = new item("Energy +50","potion",1,"Refills Energy by +50.");
var eP_100 = new item("Energy +100","potion",1,"Refills Energy by +100.");
////
////FOOD
//health - fruits
var apple = new item("Apple","food",1,"Heals player by +10.");
var orange = new item("Orange","food",1,"Heals player by +25.");
var banana = new item("Banana","food",1,"Heals player by +50.");
var pear = new item("Pear","food",1,"Heals player by +100.");
//mana - vegetables
var carrot = new item("Carrot","food",1,"Refills Mana by +10.");
var celery = new item("Celery","food",1,"Refills Mana by +25.");
var broccoli = new item("Broccoli","food",1,"Refills Mana by +50.");
var onion = new item("Onion","food",1,"Refills Mana by +100.");
//energy - snacks
var cupcake = new item("Cupcake","food",1,"Refills Energy by +10.");
var cookie = new item("Cookie","food",1,"Refills Energy by +25.");
var brownie = new item("Brownie","food",1,"Refills Energy by +50.");
var rice_crispy = new item("Rice Crispy","food",1,"Refills Energy by +100.");
////
////BOOSTERS
var hB_10 = new item("Health +10","potion",1,"Adds +10 to player Health.");
var hB_25 = new item("Health +25","potion",1,"Adds +25 to player Health.");
var hB_50 = new item("Health +50","potion",1,"Adds +50 to player Health.");
var hB_100 = new item("Health +100","potion",1,"Adds +100 to player Health.");
//mana
var mB_10 = new item("Mana +10","potion",1,"Adds +10 to player Mana.");
var mB_25 = new item("Mana +25","potion",1,"Adds +25 to player Mana.");
var mB_50 = new item("Mana +50","potion",1,"Adds +50 to player Mana.");
var mB_100 = new item("Mana +100","potion",1,"Adds +100 to player Mana.");
//energy
var eB_10 = new item("Mana +10","potion",1,"Adds +10 to player Mana.");
var eB_25 = new item("Mana +25","potion",1,"Adds +25 to player Mana.");
var eB_50 = new item("Mana +50","potion",1,"Adds +50 to player Mana.");
var eB_100 = new item("Mana +100","potion",1,"Adds +100 to player Mana.");
////
////WEAPONS
//staff
//bow
//sword
//dagger
////
////ARMOR
//helm
//shoulders
//bracers
//chest
//legs
//boots
////
//
//store all items in items Array
/*
items = list ALL items.
items[0] = potions
items[0][0] = potions -> health
items[1] = food
items[1][0] = food -> health
items[2] = boosters
items[2][0] = boosters -> health
items[3] = weapons
items[3][0] = weapons -> staff
items[4] = armor
items[4][0] = armor -> helm
*/
var items =
[
//POTIONS
[
//health
[],
//mana
[],
//energy
[],
],
//FOOD
[
//health
[],
//mana
[],
//energy
[]
],
//BOOSTERS
[
//health
[],
//mana
[],
//energy
[]
],
//WEAPONS
[
//staff
[],
//bow
[],
//sword
[],
//dagger
[]
],
//ARMOR
[
//helm
[],
//shoulders
[],
//bracer's
[],
//chest
[],
//legs
[],
//boots
[]
]
];
//push health potions
//items -> potions -> health
items[0][0].push(hP_10,hP_25,hP_50,hP_100);
console.log(items[0][0]);
The only time it displays the way i'd like it to is if I do this items[0][0].push(hP_10.name)
, but obviously that doesn't store the whole object.
Any tips are appreciated!
You have a lot of indexed data that you are referencing with an array. I would turn items into an object so as items is more readable like so.
var items =
{
"potions":{},
"food":{"cupcake":cupcake,"cookie":cookie,"brownie":brownie,"rice_crispy":rice_crispy},
"boosters":{},
"weapons":{},
"armor":{}
};
console.log(items);
console.log(items.food.cupcake.affect);
I made a mistake. when calling for object properties and methods I was using console.log(items[0][0].name)
which was really calling for the name of the sub array.
The correct syntax is console.log(items[0][0][0].name)
which will give you the name of the first object stored in the first subarray in the first array of items
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.