将对象属性键设置为变量值

时间:2016-09-21 19:57:52

标签: javascript dojo

我正在动态创建一个对象,并希望将一个属性(也是一个对象)设置为变量的值。

我的代码:

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';
//code to set the other variable values here...

var menuItem = new MenuItem({
    id: 'basic',
    label: 'Report',
    onClick: lang.hitch(this, function () {
        topic.publish('basicReportWidget/reportFromIdentify', {
            guid : {  //set this to the guid string value
                block: block,
                lot: lot,
                muni: muni,
                county: county,
                owner: owner,
                prop_loc: prop_loc,
                type: 'basic'
            }
        });
    })
});

2 个答案:

答案 0 :(得分:0)

如果所有这些都是你的代码,那么就可以像使用guid键将对象替换为guid变量一样简单,如下所示:

custId  productA    productB    productC
 101    1             1           0
 102    0             1           1

否则,如果由于某种原因无法修改该menuItem对象,则可能会通过以下方式覆盖onClick函数:

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';
//code to set the other variable values here...

var menuItem = new MenuItem({
    id: 'basic',
    label: 'Report',
    onClick: lang.hitch(this, function () {
        topic.publish('basicReportWidget/reportFromIdentify', {
            guid : guid
        });
    })
});

答案 1 :(得分:0)

正如Rayon所说,您可以使用括号表示法动态设置对象属性。

这样的事情应该有用......

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';

var menuItem = new MenuItem({
  id: 'basic',
  label: 'Report',
  onClick: lang.hitch(this, function () {
    var obj = {};
    obj[guid] = { 
      block: block,
      lot: lot,
      muni: muni,
      county: county,
      owner: owner,
      prop_loc: prop_loc,
      type: 'basic'
    };
    topic.publish('basicReportWidget/reportFromIdentify', obj);
  })
});