如何使用javascript从json键中删除空格和句点(.)



返回的对象API中包含点、空格和(.(。如何重命名这些键。

[
{
CLIENTID: 100022,
CLIENTNAME: 'DHIREN R RAJDEV',
CLIENTCODE: '1011D',
CLIENTTYPE: 'Discretionary',
ACCOUNTTYPE: 'Separate Account',
'Account Open Date': '30-06-2014',
INCEPTIONDATE: '30-06-2014',
PERFORMANCEREPORTINGDATE: '30-06-2014',
CHARGEUPTO: 43289.00011574074,
BANK ACC NUMBER.SINGLE: 239020,
BANK ACC NUMBER.DOUBLE: 789032
},{
CLIENTID: 100023,
CLIENTNAME: 'KEERTHI',
CLIENTCODE: '1011E',
CLIENTTYPE: 'Discretionary',
ACCOUNTTYPE: 'Separate Account',
'Account Open Date': '30-06-2014',
INCEPTIONDATE: '30-06-2014',
PERFORMANCEREPORTINGDATE: '30-06-2014',
CHARGEUPTO: 67893.00011574074,
BANK ACC NUMBER.SINGLE: 456781,
BANK ACC NUMBER.DOUBLE: 345234
},{
CLIENTID: 100024,
CLIENTNAME: 'RAJDEV R',
CLIENTCODE: '1011W',
CLIENTTYPE: 'Discretionary',
ACCOUNTTYPE: 'Separate Account',
'Account Open Date': '30-06-2014',
INCEPTIONDATE: '30-06-2014',
PERFORMANCEREPORTINGDATE: '30-06-2014',
CHARGEUPTO: 239076.00011574074,
BANK ACC NUMBER.SINGLE: 541234,
BANK ACC NUMBER.DOUBLE: 340987
}
]

类似于改变键";银行ACC编号单一;BANK_ ACC_NUMBER_SINGLE和BANK_。

希望这将对您有所帮助。

逻辑

  • 循环遍历对象数组并拾取单个对象
  • 循环使用该特定对象的关键帧
  • 检查该键中是否有点或空格。如果存在dor或空格,请根据要求格式化该密钥字符串,并将旧密钥的值与新密钥的值进行签名
  • 从对象中删除带点或空格的节点,并添加具有旧值的新格式化键。
    const list = [
    {
    CLIENTID: 100022,
    CLIENTNAME: 'DHIREN R RAJDEV',
    CLIENTCODE: '1011D',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 43289.00011574074,
    'BANK ACC NUMBER.SINGLE': 239020,
    'BANK ACC NUMBER.DOUBLE': 789032
    }, {
    CLIENTID: 100023,
    CLIENTNAME: 'KEERTHI',
    CLIENTCODE: '1011E',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 67893.00011574074,
    'BANK ACC NUMBER.SINGLE': 456781,
    'BANK ACC NUMBER.DOUBLE': 345234
    }, {
    CLIENTID: 100024,
    CLIENTNAME: 'RAJDEV R',
    CLIENTCODE: '1011W',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 239076.00011574074,
    'BANK ACC NUMBER.SINGLE': 541234,
    'BANK ACC NUMBER.DOUBLE': 340987
    }
    ]
    list.forEach((item) => {
    Object.keys(item).forEach((key) => {
    if (key.indexOf(' ') > -1 || key.indexOf('.') > -1) {
    newKey = key.split(' ').join('_');
    newKey = newKey.split('.').join('_');
    item[newKey] = item[key];
    delete(item[key]);
    }
    })
    })
    console.log(list)

相关内容

  • 没有找到相关文章

最新更新