我目前正在进行文字冒险。我实现了一项功能,如果你有某个项目,你可以在游戏中交换它。我只是在弄清楚如何在背包中搜索物品时遇到问题,以便正确的提示显示出来。
以下是陈述:
case "drift wood":
case "soda can":
case "shell":
case "fork":
if(mapLocation === 9)
{
gameMessage = "Weren't there some loose planks in that sunken ship?";
backpack.splice(backpackIndexNumber, 1);
}
else if(mapLocation === 9)
{
gameMessage = "That trench sure was dark.";
backpack.splice(backpackIndexNumber, 1);
}
else if(mapLocation === 9)
{
gameMessage = "That loose sand on the beach sure looks like fun to dig into.";
backpack.splice(backpackIndexNumber, 1);
}
else if(mapLocation === 9)
{
gameMessage = "A treasure chest needs unlocking.";
backpack.splice(backpackIndexNumber, 1);
}
break;
对于第一个if(mapLocation === 9),我需要程序在背包中搜索撬棍,以便让那艘沉船中有一些松散的木板?" Weren那里有一些松散的木板? #34;消息。
我的背包变量是:
var backpack = [];
答案 0 :(得分:1)
如果您的背包阵列只包含字符串" crowbar",那么您可以这样做:
hasCrowbar = backpack.indexOf("crowbar") > -1;
另一方面,如果您的背包包含这样的物体:
var backpackObj = [
{
item: "thing1",
weight: 10
},
{
item: "crowbar",
weight: 20
}
];
然后我使用lodash,并按照以下方式检查:
hasCrowbarObj = _.filter(backpackObj, function(b){
return b.item === "crowbar";
}).length > 0;
这是一个小伙伴: