Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

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

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

console.log(StatDataObj);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading