Software Ninja
Software Ninja

Reputation: 135

Convert JSON array of objects to string array using Typescript

I am using Angular 2 (latest version). I am trying to convert a JSON array i.e.

  jsonObject = [
    {"name": "Bill Gates"},
    {"name": "Max Payne"},
    {"name": "Trump"},
    {"name": "Obama"}
  ];

to a string array and store just the value in i.e

arrayList: [string] = [''];

array. I tried to use Stringify method but no luck.

Upvotes: 6

Views: 13822

Answers (1)

seidme
seidme

Reputation: 13058

This should do the job:

names: string[] = jsonObject.map(person => person.name); 

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]

Upvotes: 14

Related Questions