声明数组后更新数组中的变量 (Javascript)
Updating Variables Within Array After the Array Has been Declared (Javascript)
我在任何地方都找不到这个答案,并且已经寻找了几个月,所以如果我忽略了一些显而易见的事情,我深表歉意。自学成才,发现我的知识有一个相当令人烦恼的差距。
在一个相当复杂的连接部分中,我有两个全局范围(基本上是静态的)变量和一个位于主要 onclick 函数之外的字符类型数组,因此:
var missingWut = ["child","spouse","talisman","relic","sock"];
var rdmmissingWut;
var pronounA = "he";
var charTypes = [
["goatherd",pronounA+" wants to find a missing goat","kind"],
["shepherd",pronounA+" wants to find a missing sheep","cruel"],
["detective",pronounA+" wants to find a missing "+missingWut[rdmmissingWut],"spidery"],
...,
..., //this goes on for awhile; the array is currently 500 items long and has way more subindexes than I wanted/needed to include in this example.
];
我们已经在上面的行中声明了变量名,但显然此时 rdmmissingWut 是未定义的。
然后,为了便于记忆,我们继续在函数内部定义 rdmmissingWut,从而将其值从 undefined 更新为随机索引号:
rdmmissingWut = Math.floor(Math.random()*missingWut.length);
rdmcharType = Math.floor(Math.random()*charTypes.length);
在为字符 1 (char1) 分配随机 charType 索引之前。
var char1 = charTypes[rdmcharType];
我的问题是 -
有没有办法更新数组中的变量值 - 在我更新变量后 - 而无需重新定义整个数组?
显然可以重复数组的定义,此时它会用当前值更新所有变量值,但这看起来确实笨拙、混乱且效率低下。
另一个用例(有同样的问题):
我想使用这个相同的 chartypes 数组随机滚动字符 2 (char2) 的字符类型 - 最终也是 char3 和 char4。但是假设 char2(或 3 或 4)是女性。为此,在定义 char1 之后,我需要将 pronounA 的值更新为“她”,然后在为她选择随机 charTypes 索引之前更新 charTypes 数组中每个实例中的 pronounA 定义 - 对吗?完成此任务的最佳方法是什么?我敢肯定一定有一些我不知道的优雅解决方案。
感谢您的帮助。
每次 运行 遍历数组时,您都需要评估该变量,因此我建议使用一个可以替换为 .replaceAll
的占位符
var missingWut = ["child", "spouse", "talisman", "relic", "sock"];
var rdmmissingWut;
var pronounA = "he";
var charTypes = [
["goatherd", pronounA + " wants to find a missing goat", "kind"],
["shepherd", pronounA + " wants to find a missing sheep", "cruel"],
["detective", pronounA + " wants to find a missing _missingWut_", "spidery"]
];
function getMissingWut() {
return missingWut[rdmmissingWut || 0]; // this uses zero incase the value hasn't been updated
}
console.log(charTypes.flat().join("\n").replaceAll(/_missingWut_/g, getMissingWut()))
rdmmissingWut = 4
console.log(charTypes.flat().join("\n").replaceAll(/_missingWut_/g, getMissingWut()))
我在任何地方都找不到这个答案,并且已经寻找了几个月,所以如果我忽略了一些显而易见的事情,我深表歉意。自学成才,发现我的知识有一个相当令人烦恼的差距。
在一个相当复杂的连接部分中,我有两个全局范围(基本上是静态的)变量和一个位于主要 onclick 函数之外的字符类型数组,因此:
var missingWut = ["child","spouse","talisman","relic","sock"];
var rdmmissingWut;
var pronounA = "he";
var charTypes = [
["goatherd",pronounA+" wants to find a missing goat","kind"],
["shepherd",pronounA+" wants to find a missing sheep","cruel"],
["detective",pronounA+" wants to find a missing "+missingWut[rdmmissingWut],"spidery"],
...,
..., //this goes on for awhile; the array is currently 500 items long and has way more subindexes than I wanted/needed to include in this example.
];
我们已经在上面的行中声明了变量名,但显然此时 rdmmissingWut 是未定义的。 然后,为了便于记忆,我们继续在函数内部定义 rdmmissingWut,从而将其值从 undefined 更新为随机索引号:
rdmmissingWut = Math.floor(Math.random()*missingWut.length);
rdmcharType = Math.floor(Math.random()*charTypes.length);
在为字符 1 (char1) 分配随机 charType 索引之前。
var char1 = charTypes[rdmcharType];
我的问题是 - 有没有办法更新数组中的变量值 - 在我更新变量后 - 而无需重新定义整个数组?
显然可以重复数组的定义,此时它会用当前值更新所有变量值,但这看起来确实笨拙、混乱且效率低下。
另一个用例(有同样的问题):
我想使用这个相同的 chartypes 数组随机滚动字符 2 (char2) 的字符类型 - 最终也是 char3 和 char4。但是假设 char2(或 3 或 4)是女性。为此,在定义 char1 之后,我需要将 pronounA 的值更新为“她”,然后在为她选择随机 charTypes 索引之前更新 charTypes 数组中每个实例中的 pronounA 定义 - 对吗?完成此任务的最佳方法是什么?我敢肯定一定有一些我不知道的优雅解决方案。
感谢您的帮助。
每次 运行 遍历数组时,您都需要评估该变量,因此我建议使用一个可以替换为 .replaceAll
var missingWut = ["child", "spouse", "talisman", "relic", "sock"];
var rdmmissingWut;
var pronounA = "he";
var charTypes = [
["goatherd", pronounA + " wants to find a missing goat", "kind"],
["shepherd", pronounA + " wants to find a missing sheep", "cruel"],
["detective", pronounA + " wants to find a missing _missingWut_", "spidery"]
];
function getMissingWut() {
return missingWut[rdmmissingWut || 0]; // this uses zero incase the value hasn't been updated
}
console.log(charTypes.flat().join("\n").replaceAll(/_missingWut_/g, getMissingWut()))
rdmmissingWut = 4
console.log(charTypes.flat().join("\n").replaceAll(/_missingWut_/g, getMissingWut()))