-2

I have this array of objects

  "response":[
    {
    "No":13,
    "Name":"sadfasf asdfafddf",
    "Email":"asdfasfd@asdfasfd.com",
    },
    {
    "No":16,
    "Name":"ddfdf",
    "Email":"asdfasfd@asdfasfd.com",
    },
    {
    "No":15,
    "Name":"ddfdf",
    "Email":"Test@gmail.com",
    },

i was trying to make first letter capital

this.List.forEach( function(elm) {
             elm.Name=elm.Name.charAt(0).toUpperCase(); 
 });

but this is only showing first letter

user3653474
  • 3,393
  • 6
  • 49
  • 135
  • 2
    Does this answer your question? [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – 0stone0 Mar 13 '23 at 09:47

2 Answers2

0

Strings are immutable in JS. When you use charAt you are getting a new string, which you then make uppercase. The old string gets overwritten. This might work:

this.List.forEach(function(elm){             
   elm.Name=elm.Name.charAt(0).toUpperCase() + elm.Name.slice(1); 
});
Parvez
  • 129
  • 1
  • 7
0

If you want to capitalize each first letter of the name, "john doe" => "John Doe"...

You may try this :

    let splitter = " ";
    let response=[
        {
        "No":13,
        "Name":"sadfasf asdfafddf",
        "Email":"asdfasfd@asdfasfd.com"
        },
        {
        "No":16,
        "Name":"john doe",
        "Email":"asdfasfd@asdfasfd.com"
        },
        {
        "No":15,
        "Name":"alice in wonderland",
        "Email":"Test@gmail.com"
        }
    ]
    
    function capitalizeName(somestring){
        var str = somestring.toLowerCase();
        var table = str.split(splitter);
        var mapped = table.map((word) => word.charAt(0).toUpperCase() + word.slice(1));
        var result = mapped.join(splitter);
        return result;
    }
    for (var j=0; j<response.length; j++){
        response[j].Name = capitalizeName(response[j].Name);
    }
    for (var i=0; i<response.length; i++){
        console.log(response[i].Name);
    }
tatactic
  • 1,379
  • 1
  • 9
  • 18