使用 Undercore js 进行 Json 转换

问题描述

以下是我收到的回复

{
  "data": [
    {
      "appointment": "string","archived": true,"clinical_note_sections": [
        {
          "clinical_note_template": 0,"name": "string","values": [
            {
              "clinical_note_field": 0,"id": 0,"value": "string"
            }
          ]
        }
      ],"patient": "string"
    }
  ],"next": "string","prevIoUs": "string"
}

我想在值 Array[] 中包含约会和患者。所以最终的结果应该是这样的

{
  "data": [
    {
      /* ... */
      "values": [
        {
          "clinical_note_field": 0,"value": "string","appointment": "string","patient": "string"
        }
      ]
      /* ... */
    }
  ],"prevIoUs": "string"
}

任何使用下划线 js 可以进行此类转换的示例?

解决方法

如果纯 JS 适合你,那么...

const newProps = { "appointment": "string","patient": "string" }
data.forEach(d => d.clinical_note_sections.forEach(cns => cns.values.forEach(v => Object.assign(v,newProps))))