0

I have a code like below

function renameKey ( obj, oldKey, newKey ) {
  obj[newKey] = obj[oldKey];
  delete obj[oldKey];
}
const arr = JSON.parse(json);
arr.forEach( obj => renameKey( obj, '_id', 'id' ) );
const updatedJson = JSON.stringify( arr );

But seems like arrow function (=>) wont work in my environment and getting below error.

arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6')

It is Apigee environment and I dont have permission to change any configuration. When I remove the arrow function and calling as a normal function like below, it is failing

const arr = JSON.parse(json);
arr.forEach(renameKey( obj, '_id', 'id' ) );
const updatedJson = JSON.stringify( arr );

So, for changing the each key in the JSON, how can I use the forEach loop or it would be helpful if there is an alternate method. Could anyone please suggest.

Sridhar Adurthi
  • 131
  • 2
  • 14
  • 4
    Use a traditional function instead. – Pointy Dec 07 '21 at 18:37
  • 3
    `arr.forEach(function (obj) { renameKey(obj, '_id', 'id') });` – Nicholas Tower Dec 07 '21 at 18:38
  • Does this answer your question? [Why doesn't this arrow function work in IE 11?](https://stackoverflow.com/questions/40216015/why-doesnt-this-arrow-function-work-in-ie-11) – D M Dec 07 '21 at 18:41
  • @NicholasTower, This code was working, but got one surprising issue. It is modifying the order of the messages. Suppose if we have three JSON elements in the array, after renaming the object, the first element is becoming last and other elements are also position changing. Is there any way to avoid it? – Sridhar Adurthi Dec 20 '21 at 05:39

2 Answers2

4

If you like to use a closure with this

arr.forEach(renameKey('_id', 'id'))

pattern, you could take a closure over old and new key name and return a function which accepts the object for renaming.

function renameKey (oldKey, newKey) {
    return function (obj) {
        obj[newKey] = obj[oldKey];
        delete obj[oldKey];
    };
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    Typo? "a closure over the handed over old and new key name" – danh Dec 07 '21 at 18:49
  • @danh, what is wrong? – Nina Scholz Dec 07 '21 at 18:50
  • 1
    Thanks @DM. I see that "handed-over" means "passed parameters". Maybe "a closure over the old and new key parameters"? – danh Dec 07 '21 at 18:55
  • Similar one was coded by Nicholas as well. Thanks all for your answers. All are working perfectly and I am able to move on. – Sridhar Adurthi Dec 08 '21 at 14:50
  • 1
    @SridharAdurthi, no, because `obj` is missing in the first step and a paramter for the returned function. nicolas' approach is to use your original function with three parameters. – Nina Scholz Dec 08 '21 at 14:54
  • This is what was coded by me and it is working perfectly alright for me `function renameKey ( obj, oldKey, newKey ) {` ` obj[newKey] = obj[oldKey];` ` delete obj[oldKey];` `}` `const arr = JSON.parse(json);` `arr.forEach(function(obj) { renameKey( obj, '_id', 'id' ); } );` `const updatedJson = JSON.stringify( arr );` – Sridhar Adurthi Dec 09 '21 at 09:21
1
const arr = JSON.parse(json);
arr.forEach(function(obj){ renameKey( obj, '_id', 'id' ) });
const updatedJson = JSON.stringify( arr );
Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18