2015-02-06 62 views
1

我试图做一个简单的“选择你的冒险!”游戏,我似乎遇到了问题。我不知道如何定位我制作的这个多维数组的某些值。 我做了一个'经销商/贸易商',并有这样的销售他的物品。JavaScript抓取多维数组的某些值

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"} 
    ] 
] 

而且我有一个操作经销商的功能,比如购买物品,我不知道如何锁定某些值/数组。这就是我想要的。

function merchant() = { 
    var armor = function(slot, name, material) { 
     if(dealer[2].slot === "feet" && dealer[2].name = "Boots"} 
      money -= 10; 
     } 
    } 
} 

这应该是针对第二批服装,并寻找插槽脚和靴子的名称,对吗?

+0

貌似无效的javascript对我来说,有一个等号第一功能decleration,和'armor'是局部的作用,如何你打算打电话了吗? – adeneo 2015-02-06 18:18:42

+1

另外,数组是零索引的,所以第二个数组是'[1]' – adeneo 2015-02-06 18:19:13

+0

您可以将经销商id传递到装甲函数中。而且,索引从零开始(0)。 – 2015-02-06 18:21:26

回答

1

我会让商家数组包含商家对象。这使您可以提供有关商家的更多信息,包括商品。我有两种查找方法。第一个接受静态参数,第二个接受键值参数。

注意:我在靴子中添加了一个成本字段,因为这与您的示例有某种关系。

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>