javascript - TypeScript: Using function parameter as JSON-key name -


following situation:

setflags(data:any, key:string) {     this.flags.key= data; } 

assumed data {'foo': 'is cool'} , key 'bar' , flags should like:

{     'bar': {'foo': 'is cool'} } 

is there simple solution purpose?

edit: reply questions in comments: going typescriptclass used service, stores flags.

@injectable() export class formstoreservice {     flags:any = {};     constructor() {}     setflags(data:any, key:string) {         this.flags.key = data;     } } 

when write foo.bar, setting property named bar. if name of property dynamic, should use foo[bar] syntax. setflags method becomes

setflags(data:any, key:string) {  this.flags[key] = data; } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -