Sanchit Mahajan
Sanchit Mahajan

Reputation: 315

Ionic 3 - Want to convert array of object to array of strings in controller

In Ionic 3, I am fetching an array of objects in the controller. But it is not useful for me. So I want to convert array of objects to array of strings.. I am getting following data in controller.

   this.users = [{"id":1,"name":"US"},{"id":2,"name":"UK"}]

But I want the result below:

    this.users = ["US", "UK"];

Upvotes: 1

Views: 4600

Answers (1)

sebaferreras
sebaferreras

Reputation: 44659

You can do that in a single line like this:

this.users = this.users.map(user => user.name);

Upvotes: 8

Related Questions