请不要因为问一个“愚蠢”的问题而责备我,但我已经为此困扰了好几个星期!
为什么即使在我使用toString()将值转换为字符串之后,此数组也会使numbers转换为不同的格式?
var coords=new Array();
coords[0]=new Array(03336890,"SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1]=new Array(03346500,"EMBARRAS RIVER AT LAWRENCEVILLE, IL");
coords[2]=new Array(03612600,"OHIO RIVER AT OLMSTED");
coords[3]=new Array(05543010,"ILLINOIS RIVER AT SENECA, IL");
coords[4]=new Array(05554300,"INDIAN CREEK NEAR FAIRBURY, IL");
coords[5]=new Array(05576100,"LICK CREEK NEAR WOODSIDE, IL");
coords[6]=new Array(05576195,"SUGAR CREEK NEAR CHATHAM, IL");
coords[7]=new Array(05586300,"ILLINOIS RIVER AT FLORENCE, IL");
coords[8]=new Array(05599490,"BIG MUDDY RIVER AT RTE 127 AT MURPHYSBORO, IL");
coords[9]=new Array(411955088280601,"HANSON GRAVEL PIT AT CULVERT NEAR MORRIS, IL");
for (i=0;i<coords.length;i++){
coords[i][0].toString();
}
上查看结果
答案 0 :(得分:2)
在引号中设置数字而不是数字。
var coords=new Array();
coords[0]=new Array("03336890","SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1]=new Array("03346500","EMBARRAS RIVER AT LAWRENCEVILLE, IL");
...
答案 1 :(得分:0)
此处的问题是您将值保存为数字。 javascript会在没有前导零的情况下自动将其保存在内部,因此在您调用.toString()
之前它们会丢失。我用你给出的例子给你一个解决方法女巫用一个小技巧让你回到领先的零点。当然,如果需要,这需要一些改进:
var i, x = 100
var coords = new Array();
coords[0] = new Array(03336890, "SPOON RIVER NEAR ST. JOSEPH, IL");
coords[1] = new Array(03346500, "EMBARRAS RIVER AT LAWRENCEVILLE, IL");
coords[2] = new Array(03612600, "OHIO RIVER AT OLMSTED");
coords[3] = new Array(05543010, "ILLINOIS RIVER AT SENECA, IL");
coords[4] = new Array(05554300, "INDIAN CREEK NEAR FAIRBURY, IL");
coords[5] = new Array(05576100, "LICK CREEK NEAR WOODSIDE, IL");
coords[6] = new Array(05576195, "SUGAR CREEK NEAR CHATHAM, IL");
coords[7] = new Array(05586300, "ILLINOIS RIVER AT FLORENCE, IL");
coords[8] = new Array(05599490, "BIG MUDDY RIVER AT RTE 127 AT MURPHYSBORO, IL");
coords[9] = new Array(411955088280601, "HANSON GRAVEL PIT AT CULVERT NEAR MORRIS, IL");
for (i = 0; i < 10; i++) {
if (x < 10000000) {
alert("0" + coords[i]);
}
}
链接到jsfiddle在这里: https://jsfiddle.net/gyjjqnkt/