Home > Blockchain >  JS Using objects dinamic keynames
JS Using objects dinamic keynames

Time:06-20

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?

CodePudding user response:

The problem is that you adding string, not variable value

//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: []}
  }
}

console.log(StatDataObj)

CodePudding user response:

Try this:

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

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

console.log(StatDataObj);

  • Related