如何将在循环中创建的对象存储在Javascript中循环外定义的数组中



我有以下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])

相关内容

  • 没有找到相关文章

最新更新