As a newcomer to JavaScript, I rely heavily on eslint and plugins for guidance. My code is written for an environment with lodash 3.9.3 and must be compatible with Chrome 40, utilizing es5.
Here's a snippet of my code:
_.forEach(star.system().planets, function (world) {
if (world.starting_planet === true)
if (world.planet) {
world.planet.shuffleLandingZones = true;
} else world.generator.shuffleLandingZones = true;
});
eslint-plugin-lodash
gives me a warning about using an if statement inside a _.forEach, suggesting I use _.filter or _.some instead. The plugin directs me to its prefer-filter article.
In my opinion, using _.forEach
is the appropriate choice here since I need to set up a new value within the existing array. However, after attempting a different approach, I came up with
var startingPlanets = _.filter(star.system().planets, {
starting_planet: true,
});
_.forEach(startingPlanets, function (planets) {
if (planets.planet) {
planets.planet.shuffleLandingZones = true;
} else planets.generator.shuffleLandingZones = true;
});
I can't help but wonder if this method is less efficient as it involves iterating over two arrays instead of one. Am I overlooking something? Should I disregard the linter in this scenario? Have I misused _.filter
in achieving my objective?