-6

I followed this link: How do I dynamically assign properties to an object in TypeScript?

but it shows only how to handle object props names and values with long separate definition of an object. I wish to know how to do it with an "on the fly" anonymous built object of any type with no previous definition. I will show it in the following example.

can't make it to work. unfortunately it takes the variable name as the lable and not the variables value as I wish.

here is the code:

var srchTxtlabel: string = '';
if (this.labelType == '1')
    srchTxtlabel = 'srchtxt';
else if (this.labelType == '2')
    srchTxtlabel = 'subject';

this.myService.emit({
    srchTxtlabel : this.val1,
    'va2Label' : this.val2
})

Please help...

I.Di Zahav
  • 13
  • 3

1 Answers1

0
let emitObj = {};
emitObj[srchTxtlabel] = this.val1;

this.AdvancedSrchEvent.emit(emitObj)
Alexander_F
  • 2,831
  • 3
  • 28
  • 61
  • Thank you so much for your clear and fast answer. Though I prefer this solution in my case: [srchTxtlabel] : this.val1 . but your of course works great also – I.Di Zahav Dec 13 '17 at 08:57
  • @I.DiZahav glad I was able to help you, pleae accept the answer if you like it – Alexander_F Dec 13 '17 at 13:11