Mapping an array of Clients to an array of Client attributes needs the provided function to the map method to pick out the attributes. e.g.
Say there is the following Client class:
class Client {
clientId: Number;
clientName: string;
constructor(clientId: Number, clientName: string) {
this.clientId = clientId;
this.clientName = clientName;
}
}
And there is an initial array of Client instances.
const clientInstances : Client[] = [
new Client(1, 'Raymond'),
new Client(2, 'Damond')
]
console.log(clientInstances);
// [ Client { clientId: 1, clientName: 'Raymond' },
// Client { clientId: 2, clientName: 'Damond' } ]
The function provided to the map method is passed each client instance and a new object returned with the client attribute value set for the related key.
interface IClient {
clientName: string;
clientId: Number;
}
const clientObjects : IClient[] = clientInstances.map(
client => (
{ clientName: client.clientName, clientId: client.clientId }
)
)
console.log(clientObjects);
// [ { clientName: 'Raymond', clientId: '1' },
// { clientName: 'Damond', clientId: '2' } ]
Array.map(x=> new Array(x))does nothing useful becauseArrayis a built-in objectJSON.parse(JSON.stringify(clientArray))