[英]How do I get array in the list? with React.JS, Typescript
There is an object as有一个 object 作为
let list = [
{
id: '1',
];
and after this function is called, result should look like this;在调用此 function 后,结果应如下所示;
result = [
{
id: '6',
},
];
Step by step explaining:一步一步解释:
let list = [
{
id: '1',
name: 'Folder 1',
files: [
{id: '2', name: 'File 1'},
{id: '3', name: 'File 2'},
{id: '4', name: 'File 3'},
{id: '5', name: 'File 4'},
]
},
{
id: '6',
name: 'Folder 2',
files: [{id: '7', name: 'File 5'}],
},
];
function move (list: any[], sourceFileId: string, destinationFolderId: string): any[] {
let listIndexThatContainsTheSource = -1;
let listIndexOfDestination = list.findIndex((elem) => elem.id === destinationFolderId); //finding the destination folder index
list.forEach((elem, index) => {
if(elem.files.find((file) => file.id === sourceFileId)) {
listIndexThatContainsTheSource = index;
}
});
if(listIndexThatContainsTheSource > -1 && listIndexOfDestination > -1) { //source & destination exists
const file = list[listIndexThatContainsTheSource].files.find((file) => file.id === sourceFileId); //find the source file
list[listIndexOfDestination].files.push(file); //push source file into destination folder
list[listIndexThatContainsTheSource].files = list[listIndexThatContainsTheSource].files.filter((file) => file.id !== sourceFileId); //filter the source file
}
else {
return list; //cant find the source return original array
}
}
move(list, '4', '6');
First, you want some robust types:首先,您需要一些健壮的类型:
interface Folder {
id: string
name: string
files: FileItem[]
}
interface FileItem {
id: string
name: string
}
Now instead of List
we can use an array of folders as the type here:现在,我们可以使用文件夹数组作为此处的类型,而不是List
:
let list: Folder[] = [...]
And in order to make the arguments clearer, lets renamed them to something a little bit clearer:为了使 arguments 更清晰,让我们将它们重命名为更清晰的名称:
function move(
list: Folder[],
fileId: string,
destinationFolderId: string
): Folder[] {
//...
}
Now it's very clear the second argument is the ID of a file, and the third argument is the ID of a folder.现在很清楚第二个参数是文件的ID,第三个参数是文件夹的ID。
Then it's a pretty simple matter to find the file, remove it from it's folder, then add it to the new folder:然后找到文件,将其从文件夹中删除,然后将其添加到新文件夹中,这是一件非常简单的事情:
function move(list: Folder[], fileId: string, destinationFolderId: string): Folder[] {
// Variable to store the found file, once it is found.
let file: FileItem | undefined
// Loop through each folder looking a file with the right ID
for (const folder of list) {
// Look for the correct file in this folder.
file = folder.files.find(file => file.id === fileId)
// If a file was found in this folder...
if (file) {
// Remove it from this folder
folder.files = folder.files.filter(otherFile => otherFile.id !== fileId)
// Stop looping, there is nothing else to do.
break
}
}
// Find the destination folder by its ID.
const destinationFolder = list.find(folder => folder.id === destinationFolderId)
// If file was found, and a desination folder was found...
if (file && destinationFolder) {
// Add the file to the new destination folder.
destinationFolder.files.push(file)
}
// Return the updated folder list
return list
}
Note that this function mutates list
as a side effect.请注意,此 function 会变异list
作为副作用。 This may, or may not, be what you want.这可能是您想要的,也可能不是。 If you want a new object return without changing the one you pass in, you want to look at immutable strategies instead.如果你想要一个新的 object 返回而不改变你传入的那个,你想看看不可变的策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.