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

How to store objects created in a loop inside array defined outside of loop in 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-ing 来做到这一点:allBlocks.thisBlock.push(theseTrials[i])