1

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 to 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?

4 Answers 4

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work in older versions of some browsers like IE7/IE8 because they don't support array.map().
Good call. I've added a compatibility link.
1

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.

Comments

0
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

Comments

-1

Something like this

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

1 Comment

This isn't what they asked for. This won't generate the requested results like "200.5 K".

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.