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.

5
  • 1
    String myString = JSON.stringify(jsonObject); did not work? Commented Jan 16, 2017 at 0:29
  • @GlenPierce No unfortunately it didnt work. I tried that before. Commented Jan 16, 2017 at 0:34
  • Please print the result of JSON.stringify(jsonObject) and take a screen shot Commented Jan 16, 2017 at 0: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. Commented Jan 16, 2017 at 6:02
  • 1
    Possible duplicate of From an array of objects, extract value of a property as array Commented Jan 16, 2017 at 6:02

1 Answer 1

14

This should do the job:

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

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.