如何让一个数组的索引影响另一个数组的索引?

时间:2018-04-13 15:32:36

标签: javascript arrays

所以我现在正试图做标题状态,我有两个数组,

var weapon = ["Stick", "Dagger", "Shortsword", "Longsword", "Magical Sword", "Chunchunmaru"];

var weaponAverageDamage = [0, 3, 6, 9, 12, 15];

我想拥有它,无论具体的索引“武器”是什么,武器平台损坏都是一样的。例如,程序中武器[1]出现的任何实例,如果正在计算怪物的健康状况,那么也会访问weaponAverageDamage [1]。

1 个答案:

答案 0 :(得分:2)

考虑到数组weaponAverageDamage根据weapon数组中每个武器的相应值排序,我们可以这样做:

var weapon = ["Stick", "Dagger", "Shortsword", "Longsword", "Magical Sword", "Chunchunmaru"];
var weaponAverageDamage = [0, 3, 6, 9, 12, 15];

var weaponIndex = weapon.indexOf(weapon[2])
var AvgDamage = weaponAverageDamage[weaponIndex]; //required value