如何优化对象跟数组匹配获得新数组的算法?

后台返回的res.result
1.所有的字段是动态返回,有的条件有这个字段,有的条件没有这个字段
2.所有字段返回的结构 跟waveHeightNew一样 不再重复写了

{
  "waveHeightNew": {
    "forecastDataDTOList": [
      {
        "assignData": "",
        "date": "2022-12-31",
        "firstDay": null,
        "information": {
          "oneDay": "0.4",
          "threeDays": "0.6",
          "twoDays": "0.5"
        },
        "secondDay": null,
        "thirdlyDay": null,
        "tideFirstDay": null,
        "tideMap": null,
        "tideSecondDay": null,
        "tideThirdlyDay": null
      },
      {
        "assignData": "",
        "date": "2023-01-01",
        "firstDay": null,
        "information": {
          "oneDay": "0.4",
          "threeDays": "0.6",
          "twoDays": "0.5"
        },
        "secondDay": null,
        "thirdlyDay": null,
        "tideFirstDay": null,
        "tideMap": null,
        "tideSecondDay": null,
        "tideThirdlyDay": null
      },
      {
        "assignData": "",
        "date": "2023-01-02",
        "firstDay": null,
        "information": {
          "oneDay": "0.4",
          "threeDays": "0.6",
          "twoDays": "0.5"
        },
        "secondDay": null,
        "thirdlyDay": null,
        "tideFirstDay": null,
        "tideMap": null,
        "tideSecondDay": null,
        "tideThirdlyDay": null
      }
    ],
    "remark": "浪高(72小时)"
  },
  "windDirectionNew": {
   
  },
  "waterTempNew": {
    
  },
  "windPowerNew": {
    
  },
  "weatherNew": {
    
  },
  "weatherTempNew": {
   
  },
  "tideHightNew": {    
  
  }
}

前台维护的数据字典

data: [
        {
          type: "水温",
          id: 1,
          field: "waterTempNew"
        },

        {
          type: "浪向",
          id: 2,
          field: "tideHightNew"
        },
        {
          type: "浪高(m)",
          id: 3,
          field: "waveHeightNew"
        },
        {
          type: "天气状况",
          id: 4,
          field: "weatherNew"
        },
        {
          type: "风力",
          id: 5,
          field: "windPowerNew"

        },
        {
          type: "风向",
          id: 6,
          field: "windDirectionNew"

        },
        {
          type: "风速",
          id: 7,
          field: "windSpeedNew"

        },
        {
          type: "阵风",
          id: 8,
          field: "gustNew"

        },
        {
          type: "能见度",
          id: 9,
          field: "visibilityNew"

        }
      ],

期待两者通过匹配 field 跟 后台返回的字段 组合成下面的结构
如果该字段返回 就拼上返回的值 没有返回 就拼上 "--"

[
  {
    "type": "水温",
    "id": 1,
    "field": "waterTempNew",
    "2022-12-31": "5",
    "2023-01-01": "5",
    "2023-01-02": "5"
  },
  {
    "type": "浪向",
    "id": 2,
    "field": "tideHightNew",
    "2022-12-31": "--",
    "2023-01-01": "--",
    "2023-01-02": "--"
  },
  {
    "type": "浪高(m)",
    "id": 3,
    "field": "waveHeightNew",
    "2022-12-31": "0.4",
    "2023-01-01": "0.4",
    "2023-01-02": "0.4"
  },
  {
    "type": "天气状况",
    "id": 4,
    "field": "weatherNew",
    "2022-12-31": "晴天",
    "2023-01-01": "晴天",
    "2023-01-02": "晴天"
  },
  {
    "type": "风力",
    "id": 5,
    "field": "windPowerNew",
    "2022-12-31": "北",
    "2023-01-01": "北",
    "2023-01-02": "北"
  },

]

现在写了一个方法 else 部分有问题 另外想知道 有没有好的优化方法

 for (const key in res.result) {
            this.data.forEach(item => {
              if (key === item.field) {
                console.log(5555, key)
                res.result[key].forecastDataDTOList.map(i => {
                //重复push了导致时间跟数据重复
                  this.columns.push({
                    "title": `${i.date}`,
                    "key": `${i.date}`
                  })
                  item[i.date] = i.information.oneDay
                })
                console.log(111111, item)
              }else{
                //这里报错map undefined 
                res.result[key].forecastDataDTOList.map(i => {
                  this.columns.push({
                    "title": `${i.date}`,
                    "key": `${i.date}`
                  })
                  item[i.date] = i.information.oneDay
              }
            })
          }
阅读 1.5k
1 个回答
// 获取第一个有值的对象  dateArr = ['2022-12-31', '2023-01-01', '2023-01-02']
let dateArr = Object.fromEntries(Object.values(json).find(v => Object.prototype.toString.call(v) === '[object Object]' && Object.keys(v).length).forecastDataDTOList.map(v =>[v.date, '---']))
function getMatedata(field) {
  let days = ['oneDay', 'twoDays', 'threeDays']
  return json[field] && json[field].forecastDataDTOList ? Object.fromEntries(json[field].forecastDataDTOList.map((v, i) => [v.date, v['information'][days[i]]])) : dateArr
}
let arr1 = data.map(v => ({...v, ...getMatedata(v.field)}))
console.log(arr1)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏