我有以下 for 回圈,在其中this_block創建然后填充:
for (i = learning_trial_idx.length - 1; i >= 0; i--) {
target = Math.floor(Math.random())
these_trials = learning_trial_idx.filter(x => x.from != target)
this_block = [];
for (let i = 0; i < these_trials.length; i ) {
this_block.push(these_trials[i])
}
this_block = this_block.filter(function (a) {
var key = a.to '|' a.from;
if (!this[key]) {
this[key] = true;
return true;
}
}, Object.create(null));
}
我如何才能將this_block每次存盤到類似的東西const all_blocks = {}中,這是在 for 回圈之外定義的?
uj5u.com熱心網友回復:
您可以通過在回圈內部宣告 firstconst allBlocks = { thisBlock: [] }和push-ing來做到這一點:thisBlockforallBlocks.thisBlock.push(theseTrials[i])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437654.html
標籤:javascript for循环 全局变量
上一篇:用嵌套回圈代碼解釋二維陣列的輸出
