0

I've got very basic function

  defineSpeciality() {
        for (var i=0; i<this.readyOffer.speciality.length; i++) {
          output = output  + this.readyOffer.speciality[i].name + ', '
        }
      return output
  },

I want to be able to pass the parameter inside my function and change this.readyOffer.speciality.length into for example this.readyOffer.experience.length

so my function should be something like this:

  defineSpeciality(parameter) {
        for (var i=0; i<this.readyOffer.[parameter].length; i++) {
          output = output  + this.readyOffer.[parameter][i].name + ', '
        }
      return output
  },

but this of course does not work. How to do it?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
gileneusz
  • 1,435
  • 8
  • 30
  • 51

1 Answers1

2

To read, you should use the accessor (bracket) notation directly, without a . before:

defineSpeciality(parameter) {
  for (var i=0; i < this.readyOffer[parameter].length; i++) {        // changed this line
    output = output + this.readyOffer[parameter][i].name + ', '      // changed this line
  }
  return output
},

Reference/recommended reading: MDN - Property Accessors.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304