I want to convert number to string, buti have a problem about javascript convert number that have leading zero to octal.
I have a list of object like this
const ids = [
{ _id : 1236 },
{ _id : "abc123" },
{ _id : 012019 },
{ _id : 000010 },
{ _id : 001230 }
{ _id : 00000000000000 }
]
and i want to convert all of id in object to string
like this
[
{ _id : "1236" },
{ _id : "abc123" },
{ _id : "012345" },
{ _id : "000010 },
{ _id : "001230" },
{ _id : "00000000000000" }
]
So, i write a function like this
updateIds = () => ids.map(i => {
i._id = i._id.toString()
return i
})
updateIds()
But the result of this function is ..
[
{ _id: '1236' },
{ _id: 'abc123' },
{ _id: '012019' },
{ _id: '8' },
{ _id: '664' },
{ _id: '0' }
]
how can i ignore javascript convert 000010 and 001230 to octal ?
Ps. I think i cant use pad function to write leading zero because value of _id is dynamic. i don't know that length.
000010has become8already. You would need to handle this as text and write your own parser for it …_idvalues as strings to begin with.