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
1 Answer
Reset to default 14This should do the job:
names: string[] = jsonObject.map(person => person.name);
// => ["Bill Gates", "Max Payne", "Trump", "Obama"]