I've got this TypeScript 2 "model" that looks like this:
export class MyModel {
myProperty1: string;
myProperty2: string;
...
}
In addition to the model, I have another class defined as follows:
// Imports excluded for brevity
@Component
...
export class MyClass {
private myArray: Array<MyModel>;
ngOnInit() {
this.myArray = ...// a service call populates the array with MyModel objects;
}
ngAfterViewInit() {
var newArray: Array<string> = this.myArray ??? // extract only myProperty1 from objects in myArray and assign them to newArray
}
}
How can I extract just the myProperty1 values from myArray and populate a new array of strings?
For instance, if myArray contains two MyModel elements like below:
[{"one", "apple"}, {"two", "oranges"}]
The resulting newArray should contain these two string elements:
["one", "two"]