Skip to content
Advertisement

JS Using objects dinamic keynames

I need to use object which contains my settings, mainly keynames assignment. But I cant figure out why it does not work

//This is my object which contains names of the keys of another object
let setup={
  param1:'data1',
  param2: 'data2'
}

//So here is the main object where I need to use values as a keynames
const StatDataObj = {
  DataFields: {
    ['setup.param1']: {Blocks: [],Patch: []},
    ['setup.param1']: {Blocks: [],Patch: []}
  }
}

Everything seems quite simple but it gives me error! So what im doing wrong?

Advertisement

Answer

Try this:

const setup = { param1:'data1', param2: 'data2' };

const StatDataObj = {
  DataFields: {
    [setup.param1]: { Blocks: [], Patch: [] },
    [setup.param2]: { Blocks: [], Patch: [] }
  }
};

console.log(StatDataObj);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement