我有一个如下所示的对象数组,它可以到达未知的深度。我还有一个包含路径(键)的字符串数组。
path keys ["29be61bd-3eee-7e5d-abb2-8791c02ab842", "19be61bd-3eee-7e8d-abb2-8791c02ab841", "19be61bd-3eee-7e8d-abb2-8791c02ab841"]
[
{
"name": "background.png",
"size": 3427,
"__KEY__": "19be61bd-3eee-7e8d-abb2-8791c52av843",
"content": "02610538-5847-4d50-b2cf-d4286bda2599",
"isDirectory": false,
"dateModified": "2022-07-06T14:33:57.634Z"
},
{
"name": "background.png",
"size": 3427,
"__KEY__": "29be61bd-3eee-7e5d-abb2-8791c02ab842",
"content": "7fbdd298-85b9-4839-8f75-58e0c7543ff1",
"isDirectory": true,
"dateModified": "2022-07-06T14:33:57.634Z",
"items": [
{
"name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
"size": 3427,
"__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab841",
"isDirectory": true,
"dateModified": "2022-07-05T19:58:04.707Z",
"items": [
{
"name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
"size": 3427,
"__KEY__": "49be61bd-3eee-7e8d-abb2-8791c02ab843",
"content": "adfkasbjfkjabsfjbas",
"isDirectory": false,
"dateModified": "2022-07-05T19:58:04.707Z"
},
{
"name": "47d10e6cc15beb354a4daadb4eb6a055 (1).gif",
"size": 3427,
"__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab841",
"isDirectory": true,
"dateModified": "2022-07-05T19:58:04.707Z",
"items": [
{
"name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
"size": 3427,
"__KEY__": "19be61bd-3eee-7e8d-abb2-8791c02ab831",
"content": "dsvdsnakfna",
"isDirectory": false,
"dateModified": "2022-07-05T19:58:04.707Z"
}
]
}
]
}
]
},
{
"name": "background.png",
"size": 3427,
"__KEY__": "background.png",
"content": "4e977934-218f-4a18-a531-fc3d196bee7d",
"isDirectory": false,
"dateModified": "2022-07-06T14:33:57.634Z"
},
{
"name": "background.png",
"size": 3427,
"__KEY__": "background.png",
"isDirectory": true,
"dateModified": "2022-07-06T14:33:57.634Z",
"items": [
{
"name": "47d10e6cc15beb354a4daadb4eb6a054 (1).gif",
"size": 3427,
"__KEY__": "18be61bd-3eee-7e8d-abb2-8791c02ab831",
"content": "dsvdsnakfna",
"isDirectory": false,
"dateModified": "2022-07-05T19:58:04.707Z"
}
]
}
]
我想转到最后一个文件夹的路径(包含在字符串数组中)并更改“items”对象数组中最近添加的对象。 我如何发展这个逻辑?谁能帮我找到一种方法吗?我正在使用 Type script 来开发它,需要一种有效的方法。 如有任何帮助,我们将不胜感激。
好吧,如果我正确理解您的要求,这就是您想要做的
这是我在打字稿中的做法,因为我主要在 ts 中工作
// Start at the top level of the file structure
let currentFolder: Item[] = fileStructure;
// Iterate through each key in the provided path
for (const folderKey of path) {
// Find the folder with the current key in the current level
const currentFolderItem = currentFolder.find(folder => folder.__KEY__ === folderKey);
// Check if the folder with the current key exists
if (!currentFolderItem) {
// Handle the case where the current key is not found in the current level
return console.error(`Folder with key '${folderKey}' not found.`);
}
// Check if the current folder item is actually a folder (not a file)
if (!currentFolderItem.items) {
// Handle the case where the current item is a file and not a directory
return console.error(`Item '${folderKey}' is not a folder.`);
}
// Move to the next level (the items inside the current folder)
currentFolder = currentFolderItem.items;
}
// At this point, currentFolder contains the items array of the last folder in the path
// Now, you can modify the recently added object in the items array if it exists
if (currentFolder.length > 0) {
// Assuming the recently added object is the last item in the array
const recentlyAddedItemIndex = currentFolder.length - 1;
currentFolder[recentlyAddedItemIndex] = { ...currentFolder[recentlyAddedItemIndex], ...updatedItem };
}