Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Rename (add suffix to) every key except 2 in every object in array of objects javascript

We have the following javascript array:

[
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 }
]

and we are looking to add the suffix Agst to every key other than the team key, such that our output is:

[
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 },
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 },
  { team: 1, stat1Agst: 17, stat2Agst: 25, var1Agst: 'okay', var3Agst: 'nah', hereAgst: 24 }
]

Each object in our actual array of objects has >100 keys, and for this reason we are looking for a solution that specifically changes all key names other than team, rather than changing all keys with names ['stat1', 'stat2', 'var1', 'var3', 'here'] if possible.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can easily achieve the result using map and Object.entries as:

const arr = [
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: "okay", var3: "nah", here: 24 },
];

const result = arr.map((o) =>
  Object.fromEntries(
    Object.keys(o).map((key) =>
      key === "team" ? [key, o[key]] : [`${key}Agst`, o[key]]
    )
  )
);

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

This seems to be working okay for me.

arr = [
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 }
]

arr.forEach((row, index, theArray) => {
    let rowKeys = Object.keys(row).filter(key => key !== 'team');
    rowKeys.forEach(key => {
        theArray[index][`${key}Agst`] = theArray[index][key];
        delete theArray[index][key];
    });
});

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!