-2
const loadObject = (selectedweapon) => {
//Loads up the weapon.json
const weaponspath = path.normalize('./damage_calculatons/../Mobs and Weapons/weapons.json') 
var file_content = fs.readFileSync(weaponspath);
var weaponjson = JSON.parse(file_content);
//Searches for each weapon
selectedweapon = selectedweapon.toLowerCase()
weaponjson.forEach(mctype => {
    mctype.weapon.forEach(category => {
        category.material.forEach(material => {
            if (material.weapon.toLowerCase() === selectedweapon){
                const {weapon, damage, attackspeed, sharpness, strength} = material;
                //Renames keys in object
                let weaponUsed = {
                    NAME : weapon,
                    ATK: damage,
                    ATKSPEED: attackspeed,
                    SHARP: sharpness,
                    STR: strength
                }
                return(weaponUsed)

            }
        })
    })
})}


const weaponUsed = loadObject('Gold Sword')
console.log(weaponUsed)

I'm trying to take an array from a JSON file, find the object that is looked for in the array, clone the object with different keys, and then return the new object weaponUsed. It's supposed to show the object { NAME: 'Gold Sword', ATK: 4, ATKSPEED: 1.6, SHARP: 0, STR: 0 }, but it instead shows undefined. When I do this though

if (material.weapon.toLowerCase() === selectedweapon){
                const {weapon, damage, attackspeed, sharpness, strength} = material;
                let weaponUsed = {
                    NAME : weapon,
                    ATK: damage,
                    ATKSPEED: attackspeed,
                    SHARP: sharpness,
                    STR: strength
                }
                console.log(weaponUsed)

            }
        })
    })
})}

It shows the object { NAME: 'Gold Sword', ATK: 4, ATKSPEED: 1.6, SHARP: 0, STR: 0 }

  • Your function isn't returning anything. You need to use the `return` statement. – Matt Browne Apr 04 '21 at 03:56
  • Or to give more of a hint: the `return` statement you do have is in the wrong place. – Matt Browne Apr 04 '21 at 03:57
  • 1
    `return` doesn’t mean anything in `forEach`, other than stopping the execution of the callback in the current iteration. Use `map` or another iteration method instead. Moving the `return` won’t work, because `weaponUsed` is scoped inside the `forEach` callback. – Sebastian Simon Apr 04 '21 at 03:57
  • 2
    Duplicate of [What does `return` keyword mean inside `forEach` function?](https://stackoverflow.com/q/34653612/4642212). – Sebastian Simon Apr 04 '21 at 03:58
  • There are multiple possible solutions here, but the simplest would probably be to use `for..of` loops instead of using `.forEach()`. Note that you might need to use a transpiler like Babel if you are using `for..of` loops and you need them to work in older browsers. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of – Matt Browne Apr 04 '21 at 04:01

1 Answers1

0

try doing this

  const loadObject = (selectedweapon) => {
  //Loads up the weapon.json
  const weaponspath = path.normalize('./damage_calculatons/../Mobs and Weapons/weapons.json')
  var file_content = fs.readFileSync(weaponspath);
  var weaponjson = JSON.parse(file_content);
  //Searches for each weapon
  selectedweapon = selectedweapon.toLowerCase()
  let weaponUsed;
  weaponjson.forEach(mctype => {
    mctype.weapon.forEach(category => {
      category.material.forEach(material => {
        if (material.weapon.toLowerCase() === selectedweapon) {
          const { weapon, damage, attackspeed, sharpness, strength } = material;
          //Renames keys in object
          weaponUsed = {
            NAME: weapon,
            ATK: damage,
            ATKSPEED: attackspeed,
            SHARP: sharpness,
            STR: strength
          }

        }
      })
    })
  })
  return (weaponUsed)
}


const weaponUsed = loadObject('Gold Sword')
console.log(weaponUsed)
chesca
  • 56
  • 1
  • 2
  • 7