开发者

Modifying an array of strings in javascript

开发者 https://www.devze.com 2023-03-29 18:37 出处:网络
This is my JavaScript array [\"200.00 K\",\"200.50 K\",\"300.00 K\" ,\"300.50 K\",\"400.00 K\",\"400.50 K\"]

This is my JavaScript array

["200.00 K","200.50 K","300.00 K" ,"300.50 K","400.00 K","400.50 K"]

after parsing this array i need t开发者_开发知识库o get like this

 ["200 K","200.5 K","300 K" ,"300.5 K","400 K","400.5 K"]

and i'm using prototype

please help me?


myArray = myArray.map(function (item) {
    var n = parseFloat(item);
    return n + " K";
});

For older browsers, read this Actually, I think prototype does this for you automatically.


A variation on other answers that works in all browsers is,

var a = ["200.00 K","200.50 K","300.00 K" ,"300.50 K","400.00 K","400.50 K"];
var b = [];
for (var i = 0; i < a.length; i++)
  b.push(parseFloat(a[i]) + " K"); 

where b is the resulting array.


var a = ["200.00 K","200.50 K","300.00 K","300.50 K","400.00 K","400.50 K"];
for (var i = 0; i < a.length; i++) {
  a[i] = a[i].replace(/(?:(\.\d*[1-9])|\.)0+ /, "$1 ");
}

Afterwards, a is

200 K,200.5 K,300 K,300.5 K,400 K,400.5 K


Something like this

for(i=0;i<arrayName.length();i++){
  arrayName[i]=parseFloat(arrayName[i])+ " K";
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消