将 JSON 转换为所需格式
Transform JSON into required format
这是我的输入JSON
[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
]
我需要将 json w.r.t 父类别和所有子 json 元素 w.r.t 父类别转换为一种格式。与字符串相比,所有都应该是动态的,没有硬编码。
需要将我的输入 json 转换为以下示例格式,
[
{
"Choclate": [
{
null: [
{
"2022-05-01": {
"value": 21052631.657999996
}
},
{
"2022-04-24":{
"value": 505765
}
}
]
},
{
"ABC": [
{
"2022-05-01": {
"value": 505567
}
}
]
},
{
"XYZ": [
{
"2021-11-09": {
"value": 33796589.61000001
}
},
{
"2022-09-08": {
"value": 8000
}
}
]
}
]
},
{
"Drink":[
{
"ABC": [
{
"2022-05-01": {
"value": 882010439.286
}
}
]
},
{
"XYZ": [
{
"2022-05-01": {
"value":56100
}
}
]
}
]
}
]
我尝试将数组中的父值和子值分开并循环并推送。但没有成功。请建议我如何将我的输入 json 转换为所需的输出 json 格式。
const data =[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
let uniqueparent = [...new Set(data.map(item => item['parent']['value']))];
let uniquechild = [...new Set(data.map(item => item['child']['value']))];
console.log(uniqueparent);
console.log(uniquechild);
但是从这里开始,我不知道如何将我的数据转换成我想要的 json
您最有可能需要的是建造一棵树。非常简单的数组示例在这里:https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/
如果你想以更强大的方式使用这种结构,你可以考虑创建 class 具有“数据”、“子级”和“父级”变量的节点,然后创建一些树 class/functionality,可以与节点一起使用 add/remove 元素,跟踪它们,以所需方式打印它们等。
这对我来说很完美:
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((result, el) => {
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!result[parentName]) result[parentName] = {};
const parent = result[parentName];
if (!parent[childName]) parent[childName] = {};
const child = parent[childName];
if (!child[dateValue]) child[dateValue] = el.qty.value;
return result;
}, {});
console.log(formattedData);
如果你真的需要 "date"
属性:
中的对象,这就是我给你的建议
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((result, el) => {
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!result[parentName]) result[parentName] = {};
const parent = result[parentName];
if (!parent[childName]) parent[childName] = {};
const child = parent[childName];
if (!child[dateValue]) child[dateValue] = {value: el.qty.value};
return result;
}, {});
console.log(formattedData);
这就是你真正想要的(还有一个结果,如前面的代码片段。你可以通过在最后输入 resultMap
而不是 resultArr
来获得它):
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((resultTypes, el) => {
const resultMap = resultTypes.resultMap;
const resultArr = resultTypes.resultArr;
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!resultMap[parentName]) {
resultMap[parentName] = {};
resultArr.push({[parentName]: []})
}
const parentInMap = resultMap[parentName];
const parentInArr = resultArr.find(parents => parents[parentName])[parentName];
if (!parentInMap[childName]) {
parentInMap[childName] = {};
parentInArr.push({[childName]: []})
}
const childInMap = parentInMap[childName];
const childInArr = parentInArr.find(children => children[childName])[childName];
if (!childInMap[dateValue]) {
childInMap[dateValue] = {value: el.qty.value};
childInArr.push({[dateValue]: {value: el.qty.value}});
}
return resultTypes;
}, {resultMap: {}, resultArr: []}).resultArr;
console.log(formattedData);
这是我的输入JSON
[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
]
我需要将 json w.r.t 父类别和所有子 json 元素 w.r.t 父类别转换为一种格式。与字符串相比,所有都应该是动态的,没有硬编码。
需要将我的输入 json 转换为以下示例格式,
[
{
"Choclate": [
{
null: [
{
"2022-05-01": {
"value": 21052631.657999996
}
},
{
"2022-04-24":{
"value": 505765
}
}
]
},
{
"ABC": [
{
"2022-05-01": {
"value": 505567
}
}
]
},
{
"XYZ": [
{
"2021-11-09": {
"value": 33796589.61000001
}
},
{
"2022-09-08": {
"value": 8000
}
}
]
}
]
},
{
"Drink":[
{
"ABC": [
{
"2022-05-01": {
"value": 882010439.286
}
}
]
},
{
"XYZ": [
{
"2022-05-01": {
"value":56100
}
}
]
}
]
}
]
我尝试将数组中的父值和子值分开并循环并推送。但没有成功。请建议我如何将我的输入 json 转换为所需的输出 json 格式。
const data =[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
let uniqueparent = [...new Set(data.map(item => item['parent']['value']))];
let uniquechild = [...new Set(data.map(item => item['child']['value']))];
console.log(uniqueparent);
console.log(uniquechild);
但是从这里开始,我不知道如何将我的数据转换成我想要的 json
您最有可能需要的是建造一棵树。非常简单的数组示例在这里:https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/
如果你想以更强大的方式使用这种结构,你可以考虑创建 class 具有“数据”、“子级”和“父级”变量的节点,然后创建一些树 class/functionality,可以与节点一起使用 add/remove 元素,跟踪它们,以所需方式打印它们等。
这对我来说很完美:
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((result, el) => {
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!result[parentName]) result[parentName] = {};
const parent = result[parentName];
if (!parent[childName]) parent[childName] = {};
const child = parent[childName];
if (!child[dateValue]) child[dateValue] = el.qty.value;
return result;
}, {});
console.log(formattedData);
如果你真的需要 "date"
属性:
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((result, el) => {
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!result[parentName]) result[parentName] = {};
const parent = result[parentName];
if (!parent[childName]) parent[childName] = {};
const child = parent[childName];
if (!child[dateValue]) child[dateValue] = {value: el.qty.value};
return result;
}, {});
console.log(formattedData);
这就是你真正想要的(还有一个结果,如前面的代码片段。你可以通过在最后输入 resultMap
而不是 resultArr
来获得它):
const data = [
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 505567,
"rendered": "505.57K",
"filterable_value": "505567.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 33796589.61000001,
"rendered": "33.80M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 1638886.7959999999,
"rendered": "1.64M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1227540.6669999997,
"rendered": "1.23M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "MILLIIJK"
},
"qty": {
"value": 624,
"rendered": "624",
"filterable_value": "624.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": "USP"
},
"qty": {
"value": 19.326,
"rendered": "19.3"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 656562.255,
"rendered": "656.56K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 882010439.286,
"rendered": "882.01M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "XYZ"
},
"qty": {
"value": 56100,
"rendered": "56.10K",
"filterable_value": "56100.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 63277400.964,
"rendered": "63.28M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 100239.551,
"rendered": "100.24K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "MNO"
},
"qty": {
"value": 46763.176999999996,
"rendered": "46.76K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "THOUSAND PIECES"
},
"qty": {
"value": 52889941.824999996,
"rendered": "52.89M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Drink"
},
"child": {
"value": "rest"
},
"qty": {
"value": 42168373.635000005,
"rendered": "42.17M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 176340,
"rendered": "176.34K",
"filterable_value": "176340.0"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "ABC"
},
"qty": {
"value": 187727583.97800002,
"rendered": "187.73M"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "DEF"
},
"qty": {
"value": 33154.707,
"rendered": "33.15K"
}
},
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "FOOD"
},
"child": {
"value": "IJK"
},
"qty": {
"value": 1079,
"rendered": "1.08K",
"filterable_value": "1079.0"
}
}
];
const formattedData = data.reduce((resultTypes, el) => {
const resultMap = resultTypes.resultMap;
const resultArr = resultTypes.resultArr;
const parentName = el.parent.value;
const childName = el.child.value;
const dateValue = el.date.value;
if (!resultMap[parentName]) {
resultMap[parentName] = {};
resultArr.push({[parentName]: []})
}
const parentInMap = resultMap[parentName];
const parentInArr = resultArr.find(parents => parents[parentName])[parentName];
if (!parentInMap[childName]) {
parentInMap[childName] = {};
parentInArr.push({[childName]: []})
}
const childInMap = parentInMap[childName];
const childInArr = parentInArr.find(children => children[childName])[childName];
if (!childInMap[dateValue]) {
childInMap[dateValue] = {value: el.qty.value};
childInArr.push({[dateValue]: {value: el.qty.value}});
}
return resultTypes;
}, {resultMap: {}, resultArr: []}).resultArr;
console.log(formattedData);