【问题标题】:Column dividing for javascript objectjavascript对象的列分割
【发布时间】:2021-10-18 05:31:15
【问题描述】:

所以这是我刚刚从 SQL 数据库中获得的 javascript 对象数据:

0: {ww: "2021-27", area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", waiting_time: 5.3670104041}
1: {ww: "2021-26", area: "F15 METROLOGY", dissection_name: "DELIVERY_TIME", waiting_time: 0}
2: {ww: "2021-26", area: "F15 WET PROCESS", dissection_name: "WAIT_FOR_START_RUNNING", waiting_time: 0.1858817617}
3: {ww: "2021-25", area: "F15 CMP", dissection_name: "BATCH_WAIT_TIME", waiting_time: 0}
4: {ww: "2021-25", area: "F15 GENERAL", dissection_name: "WAIT_OTHERS", waiting_time: 0.0326702424}
5: {ww: "2021-27", area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", waiting_time: 0}
6: {ww: "2021-27", area: "F15 DIFFUSION", dissection_name: "BATCH_WAIT_TIME", waiting_time: 1.544808954}
7: {ww: "2021-24", area: "F15 METROLOGY", dissection_name: "WAIT_FOR_START_RUNNING", waiting_time: 0.0270019481}
8: {ww: "2021-24", area: "F15 PHOTO", dissection_name: "BATCH_WAIT_TIME", waiting_time: 0.0001341795}
9: {ww: "2021-24", area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", waiting_time: 0}

我想对其进行一些操作,以便每个数据块的“waiting_time”列名将更改为它的“ww”值,以像这样转换对象结构:

0: {area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", 2021-27: 5.3670104041}
    1: { area: "F15 METROLOGY", dissection_name: "DELIVERY_TIME", 2021-27: 0}
    2: { area: "F15 WET PROCESS", dissection_name: "WAIT_FOR_START_RUNNING", 2021-26: 0.1858817617}
    3: { area: "F15 CMP", dissection_name: "BATCH_WAIT_TIME", 2021-26: 0}
    4: { area: "F15 GENERAL", dissection_name: "WAIT_OTHERS", 2021-25: 0.0326702424}
    5: { area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", 2021-25: 0}
    6: {area: "F15 DIFFUSION", dissection_name: "BATCH_WAIT_TIME", 2021-24: 1.544808954}
    7: { area: "F15 METROLOGY", dissection_name: "WAIT_FOR_START_RUNNING", 2021-24: 0.0270019481}
    8: { area: "F15 PHOTO", dissection_name: "BATCH_WAIT_TIME", 2021-23: 0.0001341795}
    9: { area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", 2021-23: 0}

最终目标实际上是将转换后的数据加载到这样的角度 ag-grid 表中:

表格示例:

area dissection_name 2021-24 2021-25 2021-26 2021-27 
xxx   xxx                0     1.55  2.33    5.3670   
xxx   xxx                xxx    xxx   xxx     xxx

有没有办法做到这一点?谢谢

【问题讨论】:

    标签: javascript sql angular object ag-grid


    【解决方案1】:

    您可以使用map将数据投影成您想要的形式:

    const finalData = rawData.map((item) => {
      const { ww, waiting_time, ...dataItem } = item;
      return {
        ...dataItem,
        [ww]: waiting_time
      };
    });
    

    【讨论】:

      【解决方案2】:

      一种列操作方式

      const data = [
       {ww: "2021-27", area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", waiting_time: 5.3670104041},
       {ww: "2021-26", area: "F15 METROLOGY", dissection_name: "DELIVERY_TIME", waiting_time: 0},
       {ww: "2021-26", area: "F15 WET PROCESS", dissection_name: "WAIT_FOR_START_RUNNING", waiting_time: 0.1858817617},
       {ww: "2021-25", area: "F15 CMP", dissection_name: "BATCH_WAIT_TIME", waiting_time: 0},
       {ww: "2021-25", area: "F15 GENERAL", dissection_name: "WAIT_OTHERS", waiting_time: 0.0326702424},
       {ww: "2021-27", area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", waiting_time: 0},
       {ww: "2021-27", area: "F15 DIFFUSION", dissection_name: "BATCH_WAIT_TIME", waiting_time: 1.544808954},
       {ww: "2021-24", area: "F15 METROLOGY", dissection_name: "WAIT_FOR_START_RUNNING", waiting_time: 0.0270019481},
       {ww: "2021-24", area: "F15 PHOTO", dissection_name: "BATCH_WAIT_TIME", waiting_time: 0.0001341795},
       {ww: "2021-24", area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", waiting_time: 0},
      ]
      const arr = []
      
      data.forEach(d => {
        let obj = {}
        obj.area = d.area
        obj.dissection_name = d.dissection_name
        obj[d.ww] = d.waiting_time
        arr.push(obj)
      })
      console.log(arr)

      【讨论】:

        猜你喜欢
        • 2021-04-09
        • 2015-07-16
        • 2014-07-16
        • 2023-03-04
        • 1970-01-01
        • 2017-12-16
        • 1970-01-01
        • 2020-11-01
        • 2020-06-14
        相关资源
        最近更新 更多