最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript map function converting values to decimal - Stack Overflow

programmeradmin1浏览0评论

I have a array like this.

var elements=[5614,6619,7220,7320,7830,8220,0111,0112,0113,0142,0149]

Am converting every element to string so as to use with jquery autoplete. Am using .map function to do this.

elements = elements.map(String);

output is

["5614", "6619", "7220", "7320", "7830", "8220", "73", "74", "75", "98", "149"]

Function is taking 0111,0112,0113,0142 all these values as Octal values and converting them to decimal.

I don't want this conversation and want to preserve leading Zero also , How can I do this , please help.

I have a array like this.

var elements=[5614,6619,7220,7320,7830,8220,0111,0112,0113,0142,0149]

Am converting every element to string so as to use with jquery autoplete. Am using .map function to do this.

elements = elements.map(String);

output is

["5614", "6619", "7220", "7320", "7830", "8220", "73", "74", "75", "98", "149"]

Function is taking 0111,0112,0113,0142 all these values as Octal values and converting them to decimal.

I don't want this conversation and want to preserve leading Zero also , How can I do this , please help.

Share asked Jun 13, 2019 at 10:27 JayababuJayababu 1,6211 gold badge14 silver badges31 bronze badges 7
  • You cannot preserve a leading zero on a number type variable. – CertainPerformance Commented Jun 13, 2019 at 10:30
  • This isn't due to the string conversion, but the fact you're defining the numerical values with leading zeroes – Rory McCrossan Commented Jun 13, 2019 at 10:31
  • 1 because, as you've pointed out, a number written like 0111 is octal notation, if your input data is a number 0111, then you'll have a damned hard time "converting" that to the string "0111"` ... since you "have a array like this" declared with var elements= I would suggest you edit the code to remove leading zeros – Jaromanda X Commented Jun 13, 2019 at 10:31
  • JavaScript does not preserve the base of the number. A literal 011 will just be internally represented in IEEE 754 as a 9 and that's it. – VLAZ Commented Jun 13, 2019 at 10:31
  • 2 @greentec that does exactly what OP already has. – VLAZ Commented Jun 13, 2019 at 10:32
 |  Show 2 more ments

1 Answer 1

Reset to default 12

Function is taking 0111,0112,0113,0142 all these values as Octal values and converting them to decimal.

It's not the function doing that, it's this:

var elements=[5614,6619,7220,7320,7830,8220,0111,0112,0113,0142,0149]

In loose mode, if you start a number with a 0 followed by a series of octal digits, it's octal. That's why 010 === 8 is true:

console.log(010 === 8); // true

And heaven help us, but if you have a 0 followed by a number with non-octal decimal digits (8 or 9), it's decimal, which is why 011 === 09 and 9 === 09 are true:

console.log(011 === 09); // true
console.log(9 === 09);   // true

The solution is:

  1. Use strict mode ("use strict";). Both legacy octal literals (010) and legacy non-octal decimal literals (08) are disallowed in strict mode. (If you need to write octal, you can, with the newer 0o10 format — that's the number eight.)

  2. Don't write leading zeros on numbers (with the possible exception of a 0 just prior to a . in a fractional number less than one)

You can't fix elements after the fact (because it's impossible to know, once they're numbers, which ones were incorrectly written in octal), you have to fix it at the point you're creating it, e.g.:

var elements=[5614,6619,7220,7320,7830,220,111,112,113,142,149]
发布评论

评论列表(0)

  1. 暂无评论