0

I need to be able to input a value and get the value of the input out of an array. the problem is the array is made of objects and the input is a string, and I tried searching the internet and trying to solve the problem myself, but I could not find out how to solve the problem.

//object array
var mods = {
quick:{speed:10},
deadly:{damage:10,speed:10},
    agile:{speed:10,crit:3},
    murderous:{damage:7,speed:6,crit:3},
    slow:{speed:-15},
    sluggish:{speed:-20},
    lazy:{speed:-8},
    annoying:{damage:-20,speed:-15},
    nasty:{damage:5,speed:10,crit:2,kb:-10}
};

function calculateResult() {
      //these are strings
      var modName = document.getElementById("modName").value;
      var modType = document.getElementById("modType").value;
      //it doesnt like mixing strings and objects
      //mod is an object, modName and modType are strings
      document.getElementById("result").innerHTML =   mods.modName.modType

}
<input id="modName">
<input id="modType">
<button onClick = "calculateResult()">result</button>
<h1 id=result>a</h1>
octoCat
  • 38
  • 8
  • `mods[modName][modType]` – Konrad Jan 30 '23 at 20:30
  • some remarks: use `let` or `const` instead of `var`; `mods` is no array but an `object`; finally `var modName = document.getElementById("modName");` gives you the HtmlInputElement, so you probably want to extract the value with `var modName = document.getElementById("modName").value;` – YAMM Jan 30 '23 at 20:33
  • i know that it is an object so i will rephase this: it is a list of objects, running typeof mods gives object running mods gives [object Object] and running typeof mods.quick gives object while the value of mods,quick is undefined. typeof mods.quick.speed is a number and the value of mods.quick.speed is 10, which is the value that i am aiming to get. the modName and modType are strings. for example if i input quick for modName and speed for modType i expect to get 10 as an output, but i dont thing it likes giving it a string instad of an object, i made a mistake and i did use the .value. sorry – octoCat Jan 30 '23 at 20:45

0 Answers0