Reputation: 584
I'm trying to make a simple 'Choose Your Adventure!' game, and I seem to have run into a problem. I don't know how to target certain values of this multi-dimensional array I made. I made a 'dealer/trader' and have his items on sale like this.
var dealer = [
[
{type: "weapon", cost: 250, name: "Claymore"},
{type: "weapon", cost: 75, name: "Dagger"},
{type: "weapon", cost: 350, name: "Magic Staff"},
{type: "weapon", cost: 150, name: "Sword"},
{type: "weapon", cost: 125, name: "Bow"},
{type: "weapon", cost: 125, name: "Crossbow"},
{type: "weapon", cost: 5, name: "Arrow"},
{type: "weapon", cost: 15, name: "Bolt"}
],
[
{type: "clothing", slot: "head", name: "Helmet"},
{type: "clothing", slot: "head", name: "Hood"},
{type: "clothing", slot: "chest", name: "Chestplate"},
{type: "clothing", slot: "chest", name: "Tunic"},
{type: "clothing", slot: "chest", name: "Robe"},
{type: "clothing", slot: "leggings", name: "Legplates"},
{type: "clothing", slot: "leggings", name: "Leggings"},
{type: "clothing", slot: "leggings", slot: "Undergarments"},
{type: "clothing", slot: "feet", name: "Boots"},
{type: "clothing", slot: "feet", name: "Armored Boots"}
]
]
And I have a function that operates the dealer, such as buying an item, I don't know how to target certain values/arrays. This is what I THINK will work.
function merchant() = {
var armor = function(slot, name, material) {
if(dealer[2].slot === "feet" && dealer[2].name = "Boots"}
money -= 10;
}
}
}
That should target the second array of the clothing and look for the slot feet and name of boots, right?
Upvotes: 1
Views: 73
Reputation: 48600
I would make the merchants array contain merchant objects. This allows you to give more information about a merchant, including items. I have two find methods. The first takes static arguments, the second allows for key-value parameters.
Note: I added a cost field to your boots, as this was somehow related to your example.
var merchants = [{
name : 'Weapons Merchant',
items : [
{type: "weapon", cost: 250, name: "Claymore"},
{type: "weapon", cost: 75, name: "Dagger"},
{type: "weapon", cost: 350, name: "Magic Staff"},
{type: "weapon", cost: 150, name: "Sword"},
{type: "weapon", cost: 125, name: "Bow"},
{type: "weapon", cost: 125, name: "Crossbow"},
{type: "weapon", cost: 5, name: "Arrow"},
{type: "weapon", cost: 15, name: "Bolt"}
]
}, {
name : 'Armor Merchant',
items : [
{type: "clothing", slot: "head", name: "Helmet"},
{type: "clothing", slot: "head", name: "Hood"},
{type: "clothing", slot: "chest", name: "Chestplate"},
{type: "clothing", slot: "chest", name: "Tunic"},
{type: "clothing", slot: "chest", name: "Robe"},
{type: "clothing", slot: "leggings", name: "Legplates"},
{type: "clothing", slot: "leggings", name: "Leggings"},
{type: "clothing", slot: "leggings", name: "Undergarments"},
{type: "clothing", slot: "feet", name: "Boots", cost : 10},
{type: "clothing", slot: "feet", name: "Armored Boots"}
]
}];
function main() {
// Static approach
var armorMerchant = findMerchant(merchants, 'Armor Merchant');
var boots = findItem(armorMerchant, 'clothing', 'Boots');
print('Boots: $' + boots.cost);
// Dynamic approach
var weaponsMerchant = findMerchant(merchants, 'Weapons Merchant');
var dagger = findWithParams(weaponsMerchant.items, {type:"weapon",name:"Dagger"});
print('Dagger: $' + dagger.cost);
}
function findMerchant(merchants, name) {
return find(merchants, function(merchant) {
return merchant.name === name;
});
}
function findItem(merchant, type, name) {
return find(merchant.items, function(item) {
return item.type === type && item.name === name;
});
}
function findWithParams(arr, parameters) {
return find(arr, function(item) {
for (var parameter in parameters) {
if (item[parameter] !== parameters[parameter]) {
return false;
}
return true;
}
});
}
function find(arr, predicateFn) {
var found = null;
arr.forEach(function(item, index, items) {
if (predicateFn.apply(undefined, arguments)) {
found = item;
return true;
}
return false;
});
return found;
}
function print(text) {
document.getElementById('out').innerHTML += text + '<br />';
}
main();
<div id="out"></div>
Upvotes: 1