I have a massive object (around 10k lines) so for the sake of the question I would make an example object with three identical keys
let itemList = [{
A: {
name: `hello1`,
},
A: {
name: `hello2`,
},
A: {
name: `hello3`,
},
}];
Now in this array I am attempting to assign an integral value of each key ( by renaming them so the object turns out to be something like this:
let itemList = [{
A0: {
name: `hello1`,
},
A1: {
name: `hello2`,
},
A2: {
name: `hello3`,
},
}];
Referring to the answer here I used the following logic:
let newObject = {};
let i = 0;
for(let key in itemList){
renameObjectkey(itemList, key, key + i)
i++
}
console.log(newObject)
function renameObjectkey(object, oldName, newName){
const updatedObject = {}
for( let key in object) {
if (key === oldName) {
newObject[newName] = object[key]
} else {
newObject[key] = object[key]
}
}
object = updatedObject
}
Although it simply changes one of the values and returns me the following:
{A0: { name: `hello`}}
I am looking to achieve my goal, please suggest the most efficient way to do so since its a mega object :P
EDIT I was told to have an array instead, please assist me in a case it was an array, thanks