简体   繁体   中英

how to rename the object key in nested array of object in javascript dynamically

I need to rename the object key name dynamically in a nested array of object JavaScript, I had tried with this code. This also I got from Stack Overflow only but this is not working that means it checking first level only not doing deep replacing.

const node = {
  'id': 'de603a3e',
  'name': 'erewr',
  'subGroups': [
     {
        'id': '83d7c7e4',
        'name': 'dfds',
        'subGroups': [ ]
     },
     {
        'id': '050cde96',
        'name': 'tetwet',
        'subGroups': [ ]
     },
     {
        'id': 'd67cc4e8',
        'name': 'wewqe',
        'subGroups': [
           {
              'id': '553c301d',
              'name': 'ewqe',
              'subGroups': [

              ]
           }
        ]
     },
     {
        'id': '5d5ae5f2',
        'name': 'rwq',
        'subGroups': [
           {
              'id': 'ff29ad54',
              'name': 'wqe',
              'subGroups': [

              ]
           },
           {
              'id': '5d013943',
              'name': 'weqe',
              'subGroups': [

              ]
           }
        ]
     }
  ]
}

console.log(renameKey(node, { subGroups: 'subordinates' }))

renameKey(obj, keysMap) { 
    return transform(obj, function(result, value, key) { 
      const currentKey = keysMap[key] || key; 
      result[currentKey] = isObject(value) ? this.renameKey(value, keysMap) : value; 
    });
  }

I am using this code in TypeScript. note : transform and isObject are lodash

I am getting error ERROR TypeError: Cannot read property 'replaceKeysDeep' of undefined

This should work

function renameKeys(node, keysMaps) {
    if(!isObject(node)) return node
    if(Array.isArray(node)) return node.map(item => renameKeys(item, keysMaps))

    return Object.entries(node).reduce((result, [key, value]) => {
        const newKey = keysMaps[key] || key;
        return {
            ...result,
            [newKey]: renameKeys(value, keysMaps)

        };
    }, {});
}

 const node = { 'id': 'de603a3e', 'name': 'erewr', 'subGroups': [ { 'id': '83d7c7e4', 'name': 'dfds', 'subGroups': [ ] }, { 'id': '050cde96', 'name': 'tetwet', 'subGroups': [ ] }, { 'id': 'd67cc4e8', 'name': 'wewqe', 'subGroups': [ { 'id': '553c301d', 'name': 'ewqe', 'subGroups': [ ] } ] }, { 'id': '5d5ae5f2', 'name': 'rwq', 'subGroups': [ { 'id': 'ff29ad54', 'name': 'wqe', 'subGroups': [ ] }, { 'id': '5d013943', 'name': 'weqe', 'subGroups': [ ] } ] } ] } console.log(renameKey(node, { subGroups: 'subordinates' })) function renameKey(obj, keysMap) { return _.transform(obj, function(result, value, key) { const currentKey = keysMap[key] || key; result[currentKey] = _.isObject(value)? renameKey(value, keysMap): value; }); }
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

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