javascript - JS - 将 JSON 的嵌套数组转换为单个对象
问题描述
我想将嵌套的 JSON 结构转换为单个对象,使用我在下面的代码中尝试过的动态键,但它只适用于一个级别,我需要编写一些递归函数,我正在努力为 n 级编写代码嵌套的 JSON。请指教。
data.map((e) => {
for (let key in e) {
if (typeof e[key] === "object") {
for (let onLevel in e[key]) {
e[key + "." + onLevel] = e[key][onLevel];
}
}
}
});
例子
输入 JSON
[{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter": {
"Name": "Grocery Shopping 17",
"LocationId": "1313L0000004ENlQAM",
"Id": "0ha3L000000001qQAA",
"Location": {
"Name": "Waitrose",
"LocationType": "Site",
"Id": "1313L0000004ENlQAM"
}
}
}]
输出 JSON
[{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter.Name": "Grocery Shopping 17",
"ContactEncounter.LocationId": "1313L0000004ENlQAM",
"ContactEncounter.Id": "0ha3L000000001qQAA",
"ContactEncounter.Location.Name": "Waitrose",
"ContactEncounter.Location.LocationType": "Site",
"ContactEncounter.Location.Id": "1313L0000004ENlQAM"
}]
解决方案
正如您所说,您需要创建一个递归来更深入地了解对象。这意味着,您必须跟踪您所在的路径。
您可以通过以下方式解决它
const input = [{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter": {
"Name": "Grocery Shopping 17",
"LocationId": "1313L0000004ENlQAM",
"Id": "0ha3L000000001qQAA",
"Location": {
"Name": "Waitrose",
"LocationType": "Site",
"Id": "1313L0000004ENlQAM"
}
}
}
];
function merge( source, target = {}, ...parents) {
for (let [key, value] of Object.entries( source ) ) {
const path = (parents || []).concat( key );
if (typeof value === 'object') {
merge( value, target, ...path );
continue;
}
target[path.join('.')] = value;
}
return target;
}
console.log( merge( input[0] ) );
或者通过以下方式,您只需使用Object.assign
将更深搜索的结果分配给当前对象。
const input = [{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter": {
"Name": "Grocery Shopping 17",
"LocationId": "1313L0000004ENlQAM",
"Id": "0ha3L000000001qQAA",
"Location": {
"Name": "Waitrose",
"LocationType": "Site",
"Id": "1313L0000004ENlQAM"
}
}
}
];
function merge( source, ...parents) {
const mergedValue = {};
for (let [key, value] of Object.entries( source ) ) {
const path = (parents || []).concat( key );
if (typeof value === 'object') {
Object.assign( mergedValue, merge( value, ...path ) );
continue;
}
mergedValue[path.join('.')] = value;
}
return mergedValue;
}
console.log( merge( input[0] ) );
推荐阅读
- java - 旋转后如何正确获取3D图形的屏幕坐标
- mongodb - MongoDB 不会作为守护程序错误运行
- sql - 如何连接两个表并添加将链接 SQL Server 中的两个表的列?
- c# - Microsoft Bot Framework - 无法在模拟器中设置 BotId
- c++ - 尝试使用 std::find 时出现转换错误
- javascript - 在 IE 11 中使用 pdfmake.js 时,Uint16Array 是未定义的错误
- javascript - 越野车`` 带有 Bootstrap 和 React 的元素
- sql-server - 与 SSMS 相比,控制台 linq 查询需要更多时间
- java - 如何测量多线程应用程序中的代码性能?
- javascript - 获取数组值并添加它们(如果它们是数字)