0

i have a JSON object (sent from php) and I want to convert the IDs to a numeric key using JS. So currently it looks something like that: let foo = {"66":"test","65":"footest"};

And now I want it to look like this:

let foo = {66:"test",65:"footest"};
Mr_Tango
  • 3
  • 1

1 Answers1

0

Object keys do not need to be numerical to be accessed - as noted in the comments - they are strings regardless.- Below, I am console logging the "66" property using the brackets notation - foo[66]

let foo = {"66":"test","65":"footest"};

console.log(foo[66]); // gives "test"


// if you want to assign values numerically - ie using the index of a loop - then you could do
for(i = 63; i<65; i++) {
  foo[i] = "test" +  i;
}
console.log(foo); // gives {"63": "test63", "64": "test64","65": "footest","66": "test"}
gavgrif
  • 15,194
  • 2
  • 25
  • 27