简体   繁体   中英

How to remove space and dot (.) from json key using javascript

The object API returning containing dots, spaces, and (. ) in it. How to rename these keys.

[
 {
    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
}
]

Like change key "BANK ACC NUMBER.SINGLE" to BANK_ACC_NUMBER_SINGLE and BANK ACC NUMBER.DOUBLE to BANK_ACC_NUMBER_DOUBLE.

Hope this will help you.

Logic.

  • Loop through the array of object and pick individual objects.
  • Loop through the keys of that specific object.
  • Check whether there is a dot or a space in that key. If there is a dor or space, format that key string as per the requirement, and aassign the value of the old key to that new one.
  • Delete the node with dot or space from the object and add the new formated key with the old value.
     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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM