我需要使用包含我的設定的物件,主要是鍵名分配。但我不知道為什么它不起作用
//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: []}
}
}
一切似乎都很簡單,但它給了我錯誤!那么我做錯了什么?
uj5u.com熱心網友回復:
嘗試這個:
const setup = { param1:'data1', param2: 'data2' };
const StatDataObj = {
DataFields: {
[setup.param1]: { Blocks: [], Patch: [] },
[setup.param2]: { Blocks: [], Patch: [] }
}
};
console.log(StatDataObj);
uj5u.com熱心網友回復:
問題是你添加字串,而不是變數值
//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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492941.html
標籤:javascript 目的
