最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert JSON array of objects to string array using Typescript - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Oct 2, 2017 at 14:22 seidme 13.1k5 gold badges38 silver badges41 bronze badges asked Jan 16, 2017 at 0:27 Software NinjaSoftware Ninja 1351 gold badge2 silver badges15 bronze badges 5
  • 1 String myString = JSON.stringify(jsonObject); did not work? – Glen Pierce Commented Jan 16, 2017 at 0:29
  • @GlenPierce No unfortunately it didnt work. I tried that before. – Software Ninja Commented Jan 16, 2017 at 0:34
  • Please print the result of JSON.stringify(jsonObject) and take a screen shot – parik 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. – user663031 Commented Jan 16, 2017 at 6:02
  • 1 Possible duplicate of From an array of objects, extract value of a property as array – user663031 Commented Jan 16, 2017 at 6:02
Add a ment  | 

1 Answer 1

Reset to default 14

This should do the job:

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

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]
发布评论

评论列表(0)

  1. 暂无评论