admin管理员组

文章数量:1334948

I have an object in my Angular.io or IONIC

{name:"food panda", city:"selangor", phone:"0123456789"}

and use the following code but it's not working:

this.restaurant.name.toUpperCase();

I need to convert food panda to FOOD PANDA.

How can I do that?

I have an object in my Angular.io or IONIC

{name:"food panda", city:"selangor", phone:"0123456789"}

and use the following code but it's not working:

this.restaurant.name.toUpperCase();

I need to convert food panda to FOOD PANDA.

How can I do that?

Share Improve this question edited Mar 5, 2018 at 21:21 user9441114 asked Mar 5, 2018 at 18:26 JerryJerry 1,5831 gold badge22 silver badges43 bronze badges 3
  • 6 toUpperCase() doesn't mutate the original. You need to save it back: this.restaurant.name = this.restaurant.name.toUpperCase(); – Mark Commented Mar 5, 2018 at 18:29
  • Please share a Minimal, Complete, and Verifiable example – Ele Commented Mar 5, 2018 at 18:31
  • Possible duplicate of Swap Case on javascript – Abhijit Kar ツ Commented Mar 5, 2018 at 18:34
Add a ment  | 

3 Answers 3

Reset to default 6

toUpperCase returns a new String, it doesn't modify the original

var restaurant = { name: "food panda", city: "selangor", phone: "0123456789" };

restaurant.name.toUpperCase();

console.log(restaurant.name);           // output: food panda

var restaurantName = restaurant.name.toUpperCase();

console.log(restaurantName);            // output: FOOD PANDA

toUpperCase() creates a copy of the string and changes that one to uppercase. If you want the original string to be changed, you will need to reassign the string:

this.restaurant.name = this.restaurant.name.toUpperCase();

You have to save the resulting value, toUpperCase() does not modify anything.

var d = {name:"food panda", city:"selangor", phone:"0123456789"};
var name = d.name.toUpperCase();

Working JSFiddle

本文标签: angularJavascriptconvert value of object to uppercaseStack Overflow