0

I am trying to access a nested object based on the name of the object

I have already tried what I did below but it is giving me an error saying that "Cannot read property 'amount' of undefined" Can someone please explain to me why this doesn't work to point me in the direction of an alternative method? Thanks

var test = {
  "name": {
    "amount": "200"
  },
  "telle": {
    "amount": "150"
  }
}

function getIt(testing) {
  return test.testing.amount;
  //return test.name.amount;
}

console.log(getIt("name"))
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

1 Answers1

0
function getIt(testing) {
   return test[testing].amount;
}

That way you tell js to look for key with name stored inside testing variable.

ornic
  • 332
  • 3
  • 9