javascript - 根据项目中的要求,将 json 对象数组解析为特定格式的 json 数组
问题描述
根据我在项目中的 requiremrnt,我得到了下面的 json 数组作为输入,但我需要将其转换为特定格式的 json 对象数组。
[
{
"accident_description": "bike accident",
"reported_by": "john",
},
{
"accident_description": "car accident",
"reported_by": "sam",
}
]
输出>>>
"fields": [
{
"title": "accident_description",
"values": "bike accident"
"type": "generic",
},
{
"title": "reported_by",
"values": "john",
"type": "generic",
},
{
"title": "accident_description",
"values": "car accident"
"type": "generic",
},
{
"title": "reported_by",
"values": "sam",
"type": "generic",
},
]
解决方案
你可以像这样map
的Object.entires
每个对象:
const input=[{"accident_description":"bike accident","reported_by":"john",},{"accident_description":"car accident","reported_by":"sam",}],
type = "generic";
const output = input.flatMap(Object.entries)
.map(([title, values]) => ({ title, values, type }))
console.log(output)
推荐阅读
- google-apps-script - 谷歌表格,在一个单元格中添加不同字体大小的文本
- javascript - 如何通过 react.js 中的错误消息处理 API 代码 400 错误
- sql - SQL查询忽略表中匹配的正负值
- python - 在 Python 中使用 Selenium 检索数据的结果不一致
- python - Matplotlib 表格图例
- java - 反向迭代器和可迭代方法的Java模式?
- python - 我的累积移动平均函数不会根据参数而改变
- pyspark - 如何计算列的不同元素?
- eclipse - EGIT 无法连接到任何存储库 - 丢失未知
- amazon-web-services - Lambda Powertools Java - SegmentNotFound - @Tracing