0

How would I write a function that takes an object, and a string of the name of a key in that object and return the value of that key?

For example:

retrieveKey({name: 'Joel'}, 'name') // returns 'Joel'

retrieveKey({password: 'Fruit!'}, 'password') // returns 'Fruit!'

retrieveKey({name: 'Joel'}, 'age') // returns undefined

npgs
  • 3
  • 2

1 Answers1

0

Is this what you want?

let person1 = {name: "Joe", age: 18}

function getval(obj, key) {console.log(obj[key])}

getval(person1, "age") //returns 18

Sevada 797
  • 346
  • 1
  • 8