Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 548
Software Ninja Avatar asked Jan 16 '17 00:01

Software Ninja


1 Answers

This should do the job:

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

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]
like image 101
seidme Avatar answered Sep 21 '22 23:09

seidme