6

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.

seidme
  • 12,543
  • 5
  • 36
  • 40
Software Ninja
  • 135
  • 1
  • 2
  • 15
  • 1
    String myString = JSON.stringify(jsonObject); did not work? – Glen Pierce Jan 16 '17 at 00:29
  • @GlenPierce No unfortunately it didnt work. I tried that before. – Software Ninja Jan 16 '17 at 00:34
  • Please print the result of JSON.stringify(jsonObject) and take a screen shot – parik Jan 16 '17 at 00:38
  • 1
    There is no such thing as a "JSON array". There are just "JavaScript" arrays. There is nothing special here having to do with TypeScript or Angular. TypeScript is just a layer on top of JavaScript with type annotations and inference and checking. –  Jan 16 '17 at 06:02
  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](http://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) –  Jan 16 '17 at 06:02

1 Answers1

14

This should do the job:

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

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]
seidme
  • 12,543
  • 5
  • 36
  • 40