1

I am unable to get the data of an object

var a = {
  'ab':'cd',
  'ef':'gh',
  'ij':'kl'
}
function fun(...val){
  console.log(a.val[0])
}
fun('ab','ef')

It should output 'cd' but it is giving out error in the console any idea how do i fix this...

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79

1 Answers1

1

Use bracket notation like so:

var a = {
  'ab':'cd',
  'ef':'gh',
  'ij':'kl'
}
function fun(...val){
  console.log(a[val[0]])
}
fun('ab','ef')

Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79