将传入的 JSON 字符串转换为具有 GroupBy 功能的 Javascript 数组
我有一个返回 JSON 的 API,例如:
{
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
我正在使用 Fetch API 从 Javascript 中的 API 中检索该 JSON。
一旦我检索到它,我希望将其转换为名称/值对的 Javascript 数组,匹配特定格式如下:
[
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
所以本质上,我想要等价于:
const caseData = [
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
除了不对该数组进行编码之外,我想从动态构建它检索到的 JSON。
最终,这一切都是为了以某种格式获取我的数据,以便我可以实现类似于 https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/corlated-objects-pattern,根据“使用相关对象模式处理文档中的对象”部分。
请提供代码来说明或演示您的解决方案。
谢谢。
I have an API which returns a JSON, like:
{
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
I am retrieving that from the API in Javascript using the Fetch API.
Once I retrieve it, I wish to convert it into a Javascript array of Name/Value pairs, matching the specific format as follows:
[
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
So essentially, I want the equivalent of:
const caseData = [
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
Except instead of coding that array, I want to dynamically build it from the JSON retrieved.
This is all, ultimately, to get my data in a format so that I can implement code similar to that referenced in https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern, under the "Process objects in the document with the correlated objects pattern" section.
Please provide code to illustrate or demonstrate your solution.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
Object.entries
上使用map
you can use
map
on theObject.entries
使用 @cmgchess 提供的信息,我能够实现如下解决方案:
这会从我的 API 函数检索数据,并根据需要将其存储在数组中。
Using the information that @cmgchess provided, I was able to implement the solution as follows:
This retrieves the data from my API function, and stores it in the array as I desired.