js - 如何根据对象数组中另一个键的值更改一个键的值
我有一个像这样的对象数组:
I have an array of objects like this:
var chartData = [{count: 0, idTag: "24"}
{count: 0, idTag: "25"}
{count: 0, idTag: "26"}]
我的代码做了一些工作并生成了一个数字( totalValue
)。如果 totalValue
与 idTag
匹配,则其 count
会增加一个:
My code does some work and generates a number (totalValue
). If totalValue
matches the idTag
, its count
gets increased by one:
for (i=0; i<timesFlipped; i++) {
totalValue = runTrial();
for (j=0; j<chartData.length; j++) {
if (chartData[j].idTag === totalValue) {
chartData[j].count += 1;
break;
}
}
}
这是一个简单的例子,但 chartData
可以容纳几十个对象,而 timesFlipped
可以是数十万次迭代。
This is a simple example, but chartData
could hold a few dozen objects, and timesFlipped
can be hundreds of thousands of iterations.
这是根据匹配 idTag
更改计数
值的最佳方法>价值?有没有办法只使用正确的 idTag
值找到数组的索引而不迭代整个对象数组?
Is this the best way to change the count
value based on matching the idTag
value? Is there a way to just find the index of the array with the correct idTag
value without iterating over the whole array of objects?
如果您需要更多上下文,请点击此处我正在处理的页面: http:// blue .butler.edu / ~azazman / coupling /
Here's the page I'm working on if you want more context: http://blue.butler.edu/~aazman/coupling/
更新:
感谢您的所有建议。我根据我所看到的情况形成了自己的解决方案。关于该项目的更多背景使我得到了我的解决方案:
Thanks for all the suggestions. I kinda formed my own solution based on what I saw. A bit more background about the project which led me to my solution:
-
chartData $ c的长度$ c>和min idTag值是根据用户输入点击提交时计算的。
- 一次
min
和length
(因此已知max idTag),迭代初始化chartData
:
- The length of
chartData
and the min idTag value are calculated based on user inputs when they click submit - Once
min
andlength
are calculated (thus the max idTag is known),chartData
is iteratively initialized:
var min = 23-5 * numNickel-numPenny; // numNickel& numPenny是用户输入
var length = 5 *(2 * numNickel + 1)+ 2 * numPenny;
var tempTag = min;
for(j = 0; j< length; j ++){
chartData [j] = {count:0,idTag = tempTag};
tempTag ++;
}
- 由于特定于游戏规则的原因,idTag值(无论多少)都以
idTag:25
为对称中心(例如idTags = {24 ,25,26}或idTags = {20,21,22,23,24,25,26,27,28,29,30}。无论的长度为chartData
,长度将始终为奇数且idTag:25
将始终为[(chartData.length-1)/ 2]
'th index。
- For reasons particular to the rules of the game, the idTag values (however many there are) are symmetrically centered around
idTag:25
(e.g. idTags = {24, 25, 26} or idTags = {20,21,22,23,24,25,26,27,28,29,30}. No matter the length ofchartData
, the length will always be odd andidTag:25
will always be the[(chartData.length-1)/2]
'th index.
因此,如果idTags为 chartData
{20,21,22,23,24,25,26,27,28,29,30}
以及 runTrial()
给出 totalValue = 22
,然后 idTag:22
将始终位于索引(totalValue - min)
。
Thus, if the idTags for chartData
are {20,21,22,23,24,25,26,27,28,29,30}
and if runTrial()
gives totalValue = 22
, then idTag:22
will always be at index (totalValue - min)
.
所以我改变了chartData的初始化以及计数增加到的方式:
So I changed the initialization of chartData and the way the count gets increased to:
for (j=0; j<length; j++) {
chartData[j] = {count: 0}; //based on what I'm doing with chartData next, I need each index to still be an object
}
for (i=0; i<timesFlipped; i++) {
totalValue = runTrial();
var index = totalValue - min;
chartData[index].count++;
}
非常感谢大家的帮助:)
So thanks all for your help :)
如果您可以自由更改chartData的结构,为什么不将其设为哈希而不是数组呢?
If you're free to change the structure of chartData, why not just make it a hash instead of an array?
var chartData = {24: 0,
25: 0,
26: 0};
然后循环变为
for (i=0; i<timesFlipped; i++) {
chartData[runTrial()]++;
}