JavaScript抓取多维数组的某些值

时间:2015-02-06 18:16:33

标签: javascript arrays

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

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

那应该针对衣服的第二个阵列并寻找靴脚和靴子的名称,对吗?

1 个答案:

答案 0 :(得分: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>