【问题标题】:Parsing and Convert Data Type inside Array Angular 6在 Array Angular 6 中解析和转换数据类型
【发布时间】:2019-07-11 00:58:42
【问题描述】:

我有一个动态生成的数组,其中包含内部每个部分的字符串数据,当我尝试使用 javascript console.log(res) 打印它时,显示如下:

[{Activity: "Change Request", Total:"4427" },{ Activity: "Design Escallation", Total:"1067"}]

这是我的代码:

this.projectservice.getEventActivity().subscribe((res)=>{
    console.log(res);
})

有没有办法将我的 res 转换和解析成这样的格式:

[["Change Request", 4427],["Design Escallation", 1067]]

感谢大家的支持...

【问题讨论】:

    标签: javascript arrays json angular type-conversion


    【解决方案1】:

    您可以使用mapdestructuring assignment

    let data = [{Activity: "Change Request", Total:"4427" },{ Activity: "Design Escallation", Total:"1067"}]
    
    let op = data.map(({ Activity, Total}) => [Activity,Total])
    
    console.log(op)

    【讨论】:

    • 是的,我添加了这个 [Activity,parseInt(Total)]) 以使字符串数字成为整数数据类型
    【解决方案2】:
    this.projectservice.getEventActivity().subscribe((res)=>{
      this.result=res.map(({Activity,Total})=> [Activity,Total])
      console.log(this.result)})
    

    不会弄得一团糟,这会奏效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多