0

I have an array of information sub-divided into sub-arrays. I wish to then apply modifiers, which are also an array within an array.

  var legonisUnits = [
    "/pa/units/land/aa_missile_vehicle/aa_missile_vehicle.json",
    "/pa/units/land/assault_bot_adv/assault_bot_adv.json",
  ];
  var foundationUnits = [
    "/pa/units/air/air_factory_adv/air_factory_adv.json",
    "/pa/units/air/air_factory/air_factory.json",
  ];

  var factionUnits = [
    legonisUnits,
    foundationUnits,
  ];

  var legonisCost = [];
  var foundationCost = [];
  var effeciencyTech = [legonisCost, foundationCost];

  var costBuff = function(faction) {
    _.forEach(faction, function(unit) {
      _.forEach(unit, function(unit) {
        effeciencyTech[0].push({
          file: unit,
          path: "build_metal_cost",
          op: "multiply",
          value: 0.75,
        })
      })
    })
  }
  _.forEach(factionUnits, costBuff(factionUnits))

Where I'm stuck is how to change the array being pushed to within efficiencyTech whenever I switch array within factionUnits.

2 Answers 2

1

If you use second parameter of array iterator forEach - (i in the code below), then your task becomes much simpler:

var units = [[], []]; // replace with your own init
var efficiency = [[], []];

units.forEach((faction, i) => {
    efficiency[i] = faction.map(unit => ({
        file: unit,
        path: 'build_metal_cost',
        op: 'multiply',
        value: '0.75'
    })
));
Sign up to request clarification or add additional context in comments.

1 Comment

Ran a quick test and this seems to be precisely what I need, and much cleaner too! Unfortunately I can't use arrow operators, because es5, but that's an easy conversion.
0

The callback function in _.forEach() receives the array index as the second argument. You can use that to index into effeciancyTech.

You also shouldn't call _.forEach() inside costBuff. You're already looping over the factions because you're calling costBuff in in _.forEach().

var legonisUnits = [
  "/pa/units/land/aa_missile_vehicle/aa_missile_vehicle.json",
  "/pa/units/land/assault_bot_adv/assault_bot_adv.json",
];
var foundationUnits = [
  "/pa/units/air/air_factory_adv/air_factory_adv.json",
  "/pa/units/air/air_factory/air_factory.json",
];

var factionUnits = [
  legonisUnits,
  foundationUnits,
];

var legonisCost = [];
var foundationCost = [];
var effeciencyTech = [legonisCost, foundationCost];

var costBuff = function(faction, index) {
  _.forEach(faction, function(unit) {
    effeciencyTech[index].push({
      file: unit,
      path: "build_metal_cost",
      op: "multiply",
      value: 0.75,
    })
  })
}
_.forEach(factionUnits, costBuff)
console.log(effeciencyTech);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.min.js"></script>

2 Comments

Unfortunately this seems to result in file becoming one character from the array entry e.g. / then p then a, etc.
costBuff() doesn't need the first _.forEach(), because it's being called from _.forEach().

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.