Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Alternate for arrow function in Javascript for ForEach

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.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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];
    };
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

const arr = JSON.parse(json);
arr.forEach(function(obj){ renameKey( obj, '_id', 'id' ) });
const updatedJson = JSON.stringify( arr );
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda