0

I have an array of objects like this:

const opts_timeline = [
      {
        type: {...},
        stimulus: ''
      },
      {
        type: {...},
        stimulus: ''
      },
      {
        timeline: [{type: {...}, stimulus: ''}],
        timeline_variable: [{...}, {...}]
      },
      {
        type: {...},
        stimulus: ''
      },
    ]

The type object inside it is like this:

type: {
  info: {name: '', preloads: ''},
  stimulus: ''
}

I want to loop through this array and retrieve some properties inside type objects, I tried the forEach method:

opts_timeline.forEach(multiple_trials => {
      if (!multiple_trials.hasOwnProperty('type')) {
        multiple_trials.timeline.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);
              // ...
            }
          }
        })
      } else {
        var preloads = multiple_trials.type.info.preloads;
        if (typeof preloads !== 'undefined') {
          for (var i = 0; i < preloads.length; i++) {
            var type = multiple_trials.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);
            // ...
          }
        }
      }
    });

But this code seems redundant. Is there anyway to optimize it, or separate some of them into a helper function? Thanks!

1
  • 2
    The usual way to get rid of duplicate code is putting it into a named function. Wouldn't that work here? Commented Apr 16, 2020 at 20:56

1 Answer 1

1

seems like you can simplify this like:

  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);
          // ...
        }
      }
    })
  });

if there's a timeline, iterate that, otherwise, wrap the trial in an array and iterate that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.