javascript - 如果键值匹配,则合并并减少。重构 JSON 数据响应

标签 javascript arrays json javascript-objects

我正在尝试重构我获取的 JSON 数据。我的 JSON 响应示例如下:

{
  "sys": {
    "type": "Array"
  },
  "total": 3,
  "skip": 0,
  "limit": 100,
  "items": [
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID1",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "John",
        "lastName": "Doe",
        "title": "John's Title",
        "building": "John's Building",
        "roomNumber": 1234,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID1"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID2",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Jane",
        "lastName": "Doe",
        "title": "Jane's title",
        "building": "Jane's Building",
        "roomNumber": 4321,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID2"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID3",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Bob",
        "lastName": "Doe",
        "title": "Bob's title",
        "building": "Bob's Building",
        "roomNumber": 1111
      }
    }
  ],
  "includes": {
    "Asset": [
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID1",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "johndoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JohnDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      },
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID2",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "janedoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JaneDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      }
    ]
  }
};

我想从includes > Asset中获取fields键和属性,并将它们放入正确的item > fields > picture中具有匹配的 ASSETID。然后包含将从整个事物中删除,因为不再需要它。

这就是我想要的最终结果:

{
  "sys": {
    "type": "Array"
  },
  "total": 3,
  "skip": 0,
  "limit": 100,
  "items": [
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID1",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "John",
        "lastName": "Doe",
        "title": "John's Title",
        "building": "John's Building",
        "roomNumber": 1234,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID1"
          },
          "fields": {
            "title": "johndoe",
            "file": {
              "url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
              "details": {
                "size": 16681,
                "image": {
                  "width": 244,
                  "height": 352
                }
              },
              "fileName": "JohnDoe.jpg",
              "contentType": "image/jpeg"
            }
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID2",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Jane",
        "lastName": "Doe",
        "title": "Jane's title",
        "building": "Jane's Building",
        "roomNumber": 4321,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID2"
          },
          "fields": {
            "title": "janedoe",
            "file": {
              "url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
              "details": {
                "size": 16681,
                "image": {
                  "width": 244,
                  "height": 352
                }
              },
              "fileName": "JaneDoe.jpg",
              "contentType": "image/jpeg"
            }
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID3",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Bob",
        "lastName": "Doe",
        "title": "Bob's title",
        "building": "Bob's Building",
        "roomNumber": 1111
      }
    }
  ]
}

我目前拥有的是一个函数,它会迭代 items 中的每个项目,并尝试查看 Assets 数组是否包含来自的 ASSETID该项目。

const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);

items.forEach(item => {
  if (item.fields.picture !== undefined) {
    const {
      id
    } = item.fields.picture.sys;
    const mappedAssets = assets.map(asset => asset.sys.id);

    if (mappedAssets.includes(id)) {
      console.log('This id matched:', id)
    }
  }
})

const data = {
  "sys": {
    "type": "Array"
  },
  "total": 3,
  "skip": 0,
  "limit": 100,
  "items": [{
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID1",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "John",
        "lastName": "Doe",
        "title": "John's Title",
        "building": "John's Building",
        "roomNumber": 1234,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID1"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID2",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Jane",
        "lastName": "Doe",
        "title": "Jane's title",
        "building": "Jane's Building",
        "roomNumber": 4321,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID2"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID3",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Bob",
        "lastName": "Doe",
        "title": "Bob's title",
        "building": "Bob's Building",
        "roomNumber": 1111
      }
    }
  ],
  "includes": {
    "Asset": [{
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID1",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "johndoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JohnDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      },
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID2",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "janedoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JaneDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      }
    ]
  }
};

const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);

items.forEach(item => {
  if (item.fields.picture !== undefined) {
    const {
      id
    } = item.fields.picture.sys;
    const mappedAssets = assets.map(asset => asset.sys.id);

    if (mappedAssets.includes(id)) {
      console.log('This id matched:', id)
    }
  }
})

我不知道如何按照我想要的方式传播属性。我的方向是正确的还是我的想法有问题?

最终的解决方案是:

    data.items.forEach(item => {
      if (item.fields.picture !== undefined) {
        const { id } = item.fields.picture.sys;
        const foundAssets = data.includes.Asset.find(asset => asset.sys.id === id);
        item.fields.picture = { ...item.fields.picture, fields: {...foundAssets.fields} };
      }
    });
    delete data.includes;

最佳答案

我想可以简单地使用forEach,进行匹配,传播需要的内容并删除不需要的内容。像这样的事情

const input = {
  "sys": {
    "type": "Array"
  },
  "total": 3,
  "skip": 0,
  "limit": 100,
  "items": [{
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID1",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "John",
        "lastName": "Doe",
        "title": "John's Title",
        "building": "John's Building",
        "roomNumber": 1234,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID1"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID2",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Jane",
        "lastName": "Doe",
        "title": "Jane's title",
        "building": "Jane's Building",
        "roomNumber": 4321,
        "picture": {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "ASSETID2"
          }
        }
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "SPACEID"
          }
        },
        "id": "ENTRYID3",
        "type": "Entry",
        "createdAt": "2019-05-22T15:03:51.318Z",
        "updatedAt": "2019-05-22T15:03:51.318Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "contentTypeID"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "firstName": "Bob",
        "lastName": "Doe",
        "title": "Bob's title",
        "building": "Bob's Building",
        "roomNumber": 1111
      }
    }
  ],
  "includes": {
    "Asset": [{
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID1",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "johndoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JohnDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      },
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "SPACEID"
            }
          },
          "id": "ASSETID2",
          "type": "Asset",
          "createdAt": "2019-05-22T15:03:41.369Z",
          "updatedAt": "2019-05-22T15:03:41.370Z",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 1,
          "locale": "en-US"
        },
        "fields": {
          "title": "janedoe",
          "file": {
            "url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
            "details": {
              "size": 16681,
              "image": {
                "width": 244,
                "height": 352
              }
            },
            "fileName": "JaneDoe.jpg",
            "contentType": "image/jpeg"
          }
        }
      }
    ]
  }
};


input.items.forEach(i => {
  const foundAsset = input.includes.Asset.find(j => j.id === i.id);
  i.fields.picture = { ...i.fields.picture,
    ...foundAsset.fields
  };
});
delete input.includes;

console.log(input);

关于javascript - 如果键值匹配,则合并并减少。重构 JSON 数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60514303/

相关文章:

python - 选择numpy数组不同行的不同列的元素

javascript - 使用lodash groupBy函数对数组中的对象进行分类

javascript - 在javascript中更改数组位置

javascript - 将字符串数据转为json格式

php - jquery ajax 返回值没有按预期响应

javascript - "value"在 d3 力定向图中代表什么

javascript - API 数据显示在浏览器的控制台中,但不显示在网页上 - Vue.js

json - HttpRequestData 将正文包装在 { "json": "" }

javascript - 使用 Angular js ng-repeat 指令过滤 json 对象

javascript - 尝试理解 Vue.js 中的 "undefined"警告