Javascript 在这个对象数组中循环的最佳方式是什么?

Javascript 在这个对象数组中循环的最佳方式是什么?,javascript,Javascript,我有这样一个对象数组: const opts_timeline = [ { type: {...}, stimulus: '' }, { type: {...}, stimulus: '' }, { timeline: [{type: {...}, stimulus: ''}], timeline_variable: [{...}, {...

我有这样一个对象数组:

const opts_timeline = [
      {
        type: {...},
        stimulus: ''
      },
      {
        type: {...},
        stimulus: ''
      },
      {
        timeline: [{type: {...}, stimulus: ''}],
        timeline_variable: [{...}, {...}]
      },
      {
        type: {...},
        stimulus: ''
      },
    ]
其中的
类型
对象如下:

const opts_timeline = [
      {
        type: {...},
        stimulus: ''
      },
      {
        type: {...},
        stimulus: ''
      },
      {
        timeline: [{type: {...}, stimulus: ''}],
        timeline_variable: [{...}, {...}]
      },
      {
        type: {...},
        stimulus: ''
      },
    ]
类型:{
信息:{name:'',预加载:'},
刺激:''

}
似乎您可以将其简化为:

  opts_timeline.forEach(multiple_trials => {
    (multiple_trials.timeline || [multiple_trials]).forEach(trial => {
      var preloads = trial.type.info.preloads;
      if (typeof preloads !== 'undefined') {
        for (var i = 0; i < preloads.length; i++) {
          var type = trial.type.info.name;
          var param = preloads[i].parameter;
          var media = preloads[i].media_type;
          var func = preloads[i].conditional_function;
          var trials = timeline.trialsOfType(type);
          // ...
        }
      }
    })
  });
opts\u timeline.forEach(多个试验=>{
(多个| | |[多个|试验])forEach(试验=>{
var PRELOSS=试用版.type.info.preloads;
if(预加载类型!==“未定义”){
对于(变量i=0;i

如果有时间线,则迭代该时间线,否则,将试验包装在数组中并迭代该时间线。

消除重复代码的常用方法是将其放入命名函数中。这不管用吗?