Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda