我有这种类型的数组:
0: Client
clientId: 405229
clientName: "Test, Jamie"
1: Client
clientId: 405288
clientName: "Test1, Jamie"
2: Client
clientId: 405239
clientName: "Test3, Jamie"
我基本上想将它转换为一个没有类的普通数组
0:
clientId: 405229
clientName: "Test, Jamie"
1:
clientId: 405288
clientName: "Test1, Jamie"
2:
clientId: 405239
clientName: "Test3, Jamie"
我尝试过这样做:
Array.map(x=> new Array(x))
但是会产生相同的结果。
有什么帮助吗?
最佳答案
这是一个很好的函数式 ES6 风格的实现方式:
// Make the typed array
const clients : Array<Client> = [];
for ( let i = 0; i < 10; i++ ) {
clients.push ( new Client ( i, 'client_' + i.toString () ) );
}
// This is the magic line, just spread the object
const plain = clients.map ( x => ( { ...x } ) );
// First logs as a typed array,
// second as just plain old objects
console.log ( clients );
console.log ( plain );
关于javascript - 更改 typescript 中数组的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450792/