gpt4 book ai didi

javascript - Nodejs,如何重构数据的响应?

转载 作者:行者123 更新时间:2023-12-04 17:11:09 24 4
gpt4 key购买 nike

我如何根据 重新构造数据的响应层级 parentfoldersid ?请考虑父文件夹可以是多个,子文件夹可以创建另一个文件夹。我用最简单的例子来理解我的意思,
注意:请参阅下面所需的回复,谢谢
我的代码

        ...
const res1 = results.rows
let newjson = [];
for (let x =0; x < res1.length ; x++)
{
newjson[x] = {
id :res1[x]['id'],
name :res1[x]['name'],
hierarchylevel :res1[x]['hierarchylevel'],
parentfoldersid :res1[x]['parentfoldersid']
}
}
res.status(200).json(newjson);
....
实际结果
    {
id: 442,
name: "Leadership1",
hierarchylevel: 1,
parentfoldersid: 0,
},
{
id: 475,
name: "testv1",
hierarchylevel: 2,
parentfoldersid: 442
},
{
id: 476,
name: "testv2",
hierarchylevel: 2,
parentfoldersid: 442,
},
{
id: 539,
name: "testv2",
hierarchylevel: 3,
parentfoldersid: 476
}
我想要的结果
{
id: 442,
name: "Leadership1",
hierarchylevel: 1,
parentfoldersid: 0, //parentfolder
children: [
{
id: 475, //subfolder
name: "testv1",
hierarchylevel: 2,
parentfoldersid: 442
},
{
id: 476, //subfolder
name: "testv2",
hierarchylevel: 2,
parentfoldersid: 442,
children: [
{
id: 539, //subfolder created inside the subfolder
name: "testv2",
hierarchylevel: 3,
parentfoldersid: 476
}
]
},
]
}
我用了穆罕默德的逻辑
这就是结果
[
{
"trfoldersid": 442,
"name": "Leadership1",
"parentfoldersid": 0,
"hierarchylevel": 1,
"childrens":[
{
"trfoldersid": 475,
"name": "testv1",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[]
},
{
"trfoldersid": 476,
"name": "testv2",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[{"trfoldersid": 539, "name": "testv2%20sub%20%20folder%201", "parentfoldersid": 476, "awsobjectkey": "",…]
}
]
},
{
"trfoldersid": 540,
"name": "Leadership2",
"parentfoldersid": 0,
"hierarchylevel": 1,
"childrens":[]
},
{
"trfoldersid": 475,
"name": "testv1",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[]
},
{
"trfoldersid": 476,
"name": "testv2",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[
{"trfoldersid": 539, "name": "testv2%20sub%20%20folder%201", "parentfoldersid": 476, "awsobjectkey": "",…}
]
}
]
这是正确的结果
[
{
"id": 442,
"name": "Leadership1",
"parentfoldersid": 0,
"hierarchylevel": 1,
"childrens":[
{
"id": 475,
"name": "testv1",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[]
},
{
"id": 476,
"name": "testv2",
"parentfoldersid": 442,
"hierarchylevel": 2,
"childrens":[{"id": 539, "name": "testv2%20sub%20%20folder%201", "parentfoldersid": 476]
}
]
},
},
{
"id": 540,
"name": "Leadership2",
"parentfoldersid": 0,
"hierarchylevel": 1,
"childrens":[]
},
]
enter image description here

最佳答案

我的想法很简单:遍历您的响应数组并返回一个新数组 resNew .
首先,按 hierarchylevel 对响应数组进行排序,这使您能够推送/更新您的 resNew当您从一个文件夹转到另一个文件夹时。这也将确保具有较低 hierarchylevel 的文件夹(即更接近根文件夹)插入 resNew在他们的子文件夹之前。
当您遍历 res 时数组,在 for 中循环,检查文件夹是否是顶级文件夹(即 parentfoldersid == 0hierarchylevel == 1 )。
如果当前文件夹是顶级文件夹,只需将该文件夹插入 resNew .另一方面,如果文件夹不是顶级文件夹,则必须将其插入 resNew使用 insertJSON()功能。insertJSON()函数更新您的 resNew通过在 for 中插入当前文件夹循环到正确的父文件夹。该函数采用 2 个参数 - 第一个参数是您要插入的文件夹,第二个参数是 resNew 的当前状态.
insertJSON()函数,首先转换当前文件夹和当前resNew到 JSON 字符串,以便您可以使用正则表达式查找正确的父文件夹并将当前文件夹插入其 "childrens":[属性(property)。插入后,函数返回更新的resNew作为对象。

var res = [
{
id: 442,
name: 'Leadership1',
hierarchylevel: 1,
parentfoldersid: 0,
},
{
id: 539,
name: 'testv2',
hierarchylevel: 3,
parentfoldersid: 476,
},
{
id: 475,
name: 'testv1',
hierarchylevel: 2,
parentfoldersid: 442,
},
{
id: 476,
name: 'testv2',
hierarchylevel: 2,
parentfoldersid: 442,
},
];

res = res.sort(function (a, b) {
return a.hierarchylevel - b.hierarchylevel;
});

var resNew = [];

for (var i = 0; i < res.length; i++) {
var obj = res[i];
obj.childrens = []; // you never know if it has children or not

if (obj.parentfoldersid === 0) {
resNew.push(obj);
} else {
resNew = insertJSON(obj, resNew);
}
}

function insertJSON(what, where) {
var objJSON = JSON.stringify(what);
var resJSON = JSON.stringify(where);

var regexStr =
`(.*?"id":` +
what.parentfoldersid +
`,.*,"hierarchylevel":` +
(what.hierarchylevel - 1) +
`,"parentfoldersid":\\d+,"childrens":\\[)`;
var regex = new RegExp(regexStr, 'gm');
var resArr = resJSON.split(regex);
resJSON = (resArr[1] + objJSON + resArr[2]).replace(/}{/g, '},{');
return JSON.parse(resJSON);
}

console.log(resNew);

关于javascript - Nodejs,如何重构数据的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69423690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com