13

I have JavaScript Array that store String variable in it. I have tried below code that help me to convert Javascript variable to uppercase letter,

<html>
<body>

    <p id="demo"></p>

    <button onclick="toUppar()">Click Here</button>

    <script>
    Array.prototype.myUcase=function()
    {
        for (i=0;i<this.length;i++)
          {
          this[i]=this[i].toUpperCase();
          }
    }

    function toUppar()
    {
        var numArray = ["one", "two", "three", "four"];
        numArray.myUcase();
        var x=document.getElementById("demo");
        x.innerHTML=numArray;
    }
    </script>

</body>
</html>

but i want to convert only first character of Javascript Variable to Upper case.

Desired output : One,Two,Three,Four

Charles
  • 50,943
  • 13
  • 104
  • 142
Vijay
  • 8,131
  • 11
  • 43
  • 69
  • `@thgaskell` i need to with JavaScript array not directly for java script variable. – Vijay Aug 31 '13 at 09:26
  • In that case take a look at [`Array.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) :) – thgaskell Aug 31 '13 at 12:08

4 Answers4

5

If you need the upper case for presentation to your views, you can simply use css for do so!

div.capitalize:first-letter {
  text-transform: capitalize;
}

here is the complete fiddle example: http://jsfiddle.net/wV33P/1/

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
4

Use this extension (as per previous SO-answer):

String.prototype.first2Upper = String.prototype.first2Upper || function(){
 return this.charAt(0).toUpperCase()+this.slice(1);
}
//usage
'somestring'.first2Upper(); //=> Somestring

And for your array using map in combination with this extension would be:

var numArray = ["one", "two", "three", "four"]
               .map(function(elem){return elem.first2Upper();});
// numArray now: ["One", "Two", "Three", "Four"]

See MDN for explanation of and shim for the map method

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

You are almost there. Instead of uppercasing the entire string, uppercase only the first character.

Array.prototype.myUcase = function()
{
    for (var i = 0, len = this.length; i < len; i += 1)
    {
          this[i] = this[i][0].toUpperCase() + this[i].slice(1);
    }
    return this;
}

var A = ["one", "two", "three", "four"]
console.log(A.myUcase())

Output

[ 'One', 'Two', 'Three', 'Four' ]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2
Array.prototype.ucfirst = function () {

    for (var len = this.length, i = 0; i < len; i++) {

        if (Object.prototype.toString.call(this[i]) === "[object String]") {
            this[i] = (function () {
                return this.replace(
                    /\b([a-z])[a-z]*/ig,
                    function (fullmatch, sub1) {
                        return sub1.toUpperCase() + fullmatch.slice(1).toLowerCase();
                    }
                );
            }).call(this[i]);
        }

    }
    return this;
};

console.log(["conVertInG", "fIRST", "ChaRcteR", "OF", new Array, String, new String("string tO UPPER CASE [duPLicatE]")].ucfirst());
//
// ["Converting", "First", "Charcter", "Of", [], String(), "String To Upper Case [Duplicate]"]
//
public override
  • 974
  • 8
  • 17