Getting string from variable (from imported JS) as key in object Getting string from variable (from imported JS) as key in object vue.js vue.js

Getting string from variable (from imported JS) as key in object


You can use es6 computed properties:

obj: {  [cons.FILENAMEA]: {},  [cons.FILENAMEB]: {    children: [      cons.CHILDFILENAME1,      cons.CHILDFILENAME2,      cons.CHILDFILENAME3    ]  }}

The expression inside the brackets will be evaluated and used as the property name


An alternative to @Dan's answer is to declare the object before hand and add the key/value pairs to it

export default {  data () {    const obj = {}    obj[cons.FILENAMEA] = {}    obj[cons.FILENAMEB] = {          children: [            cons.CHILDFILENAME1,            cons.CHILDFILENAME2,            cons.CHILDFILENAME3          ]     }    return {      cons: cons,      obj     }    }  }