我有以下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循环之外定义的?
您可以通过在for
循环内的thisBlock
内声明第一个const allBlocks = { thisBlock: [] }
和push
来实现这一点:allBlocks.thisBlock.push(theseTrials[i])