javascript - 更改 typescript 中数组的类型

标签 javascript arrays typescript

我有这种类型的数组:

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 );

Trace of arrays

关于javascript - 更改 typescript 中数组的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450792/

相关文章:

javascript - "clock"指令的 Angular 单元测试 $interval

python - 使用字典和数组将点符号字符串转换为嵌套的 Python 对象

cordova - 如何在 ionic 2 中使图标和按钮变大

typescript - 为什么 typescript 会在不相关的类之间隐式转换?

javascript - Webpack typescript 导入未定义

javascript - 获取 Bootstrap 选择选择器实时搜索的输入文本值

javascript - 带有 jQ​​uery 链接的 ASP.NET MVC 列表

javascript - nodejs 函数中的可选参数

arrays - 如何使用 Swift 获取 UITableView Cell 的标签文本?

arrays - 矩阵数组部分的最大和