3

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.

6
  • you could have a look here: stackoverflow.com/q/58336825/1447675 Commented Oct 22, 2019 at 11:29
  • btw, leading zeros are gone after converting. Commented Oct 22, 2019 at 11:30
  • 2
    “I have a list of object like this” - as actual JS code? Then you lose your original values at the time of parsing already - log that to console, and you’ll see that 000010 has become 8 already. You would need to handle this as text and write your own parser for it … Commented Oct 22, 2019 at 11:32
  • Where does that array of ids come from? Is it generated somewhere? If so, you should adapt the code to generate the _id values as strings to begin with. Commented Oct 22, 2019 at 11:51
  • 2
    How do you do the transformation from CSV to the object structure that you have in your question? It's in that bit of code that I recommend you to make a change so that the id values will remain as string instead of numbers. Commented Oct 22, 2019 at 12:01

2 Answers 2

4

You have to define the base you want to use in the toString call.

If you want the ids to be parsed as base 10, then called toString(10). It's impossible to do the padding to preserve the length because numbers are numbers, so leading zeros are ignored. You can pad to a fixed length, for all the ids:

updateIds = () => ids.map(i => {
  i._id = i._id.toString(10).padStart(9, '0');
  return i
})
Sign up to request clarification or add additional context in comments.

Comments

1

Use Constructor call Number("001230"). It will convert all numbers to base 10 only, so you won't have the octal conversion problem

const ids = [
  { _id : 1236 },
  { _id : "abc123" },
  { _id : 012019 },
  { _id : 000010 },
  { _id : 001230 },
  { _id : 00000000000000 }
];

let ans = [];

ids.forEach((val)=>{

    let obj = {};
    obj._id = Number(val["_id"]);
    ans.push(obj);

})

console.log(ans);

1 Comment

“Use Constructor call Number("001230")” - only that’s not possible here, because you do not have that value 001230 any more. Log ids to console, and you will see that 001230 has become 664 in there already. This happens at the step of parsing the object literal already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.