JSFiddle - React, Tailwind, and code Playground

by Nalin Sajwan

JavaScript

const testArr = [
  {
    id: 1,
    name: {
      first: "Audrey",
      last: "Grigoletti",
    },
    email: "[email protected]",
    gender: "Male",
    ip_address: "67.209.54.105",
  },
  {
    id: 2,
    name: {
      first: "Eberhard",
      last: "Melin",
    },
    email: "[email protected]",
    gender: "Non-binary",
    ip_address: "220.146.87.244",
  },
];

const testArr2 = JSON.parse(JSON.stringify(testArr));

const newData = testArr.map(data => ({
  ...data,
}));

newData.forEach((data, index) => {
  data.name.first = `${index}-${data.name.first}`;
  data.name.last = `${index}-${data.name.last}`;
})

console.log('\n\ntestArr ==> ', JSON.stringify(testArr, null, 2));
console.log('newData ==> ', JSON.stringify(newData, null, 2));

const newClonedData = testArr2.map(data => JSON.parse(JSON.stringify(data)));

newClonedData.forEach((data, index) => {
  data.name.first = `${index}-${data.name.first}`;
  data.name.last = `${index}-${data.name.last}`;
})

console.log('\n\ntestArr2 ==> ', JSON.stringify(testArr2, null, 2));
console.log('newClonedData ==> ', JSON.stringify(newClonedData, null, 2));