将对象从一个数组移动到另一个数组



我需要在Typescript中将项从一个数组移动到另一个数组。但这个问题与以往不同。我需要将一个对象从另一个对象中的数组转移到另一个对象中的另一个数组:)

这是我的车票。ts文件
type List = unknown;
export default function move(list: List, source: string, destination: string): List {
}

这是规范文件

import move from './transfer';
describe('move', () => {
it('moves given file to another folder', () => {
const 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' }],
},
];
const result = [
{
id: '1',
name: 'Folder 1',
files: [
{ id: '2', name: 'File 1' },
{ id: '3', name: 'File 2' },
{ id: '5', name: 'File 4' },
],
},
{
id: '6',
name: 'Folder 2',
files: [
{ id: '7', name: 'File 5' },
{ id: '4', name: 'File 3' },
],
},
];
expect(move(list, '4', '6')).toStrictEqual(result);
});
it('throws error if given source is not a file', () => {
const list = [
{
id: '1',
name: 'Folder 1',
files: [{ id: '2', name: 'File 1' }],
},
{ id: '3', name: 'Folder 2', files: [] },
];
expect(() => move(list, '3', '1')).toThrow('You cannot move a folder');
});
it('throws error if given destination is not a folder', () => {
const list = [
{
id: '1',
name: 'Folder 1',
files: [{ id: '2', name: 'File 1' }],
},
{ id: '3', name: 'Folder 2', files: [{ id: '4', name: 'File 2' }] },
];
expect(() => move(list, '2', '4')).toThrow('You cannot specify a file as the 
destination');
});
});

我需要创建"type List"并按照规范文件中所示的方式移动项。

我该怎么做呢?

为了提高代码的可读性,我尽可能地划分了函数,如果有什么不清楚的地方请不要介意问!


// type definitions
type CustomFile = { id: string; name: string };
type Folder = {
id: string;
name: string;
files: CustomFile[];
};
// --------
const list: Folder[] = [
{
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" }]
}
];

// this function will recognize why file cannot be found inside the list
function recognizeFileSearchError(list: Folder[], fileId: string, destinationId: string): never {
// if folder has id equal to source file id
const folderIndex = list.findIndex(fld => fld.id === fileId)
if (folderIndex !== -1) throw new Error("You cannot move a folder")

// if destination has id equal to any file id
const destinationIndex = list.findIndex(fld => fld.files.some(file => file.id === destinationId))
if (destinationIndex !== -1) throw new Error("You cannot specify a file as the destination")
// any other case
throw new Error("File not found")
}

// this function will find all the necessary indices 
function findIndicesByFileId(list: Folder[], sourceId: string, destinationId: string): [sourceFolderIndex: number, destinationFolderIndex: number, fileIndex: number,] {
// finding folder id (is any folder that has the file with the serched id inside)
const folderIndex = list.findIndex(fld => fld.files.some(file => file.id === sourceId))
//finding destination folder
const destinationFolderIndex = list.findIndex(fld => fld.id === destinationId)
//if any search fails recognizeFileSearchError function runs
if (folderIndex === -1 || destinationFolderIndex === -1) recognizeFileSearchError(list, sourceId, destinationId)

// finding file index inside the designed folder
const fileIndex = list[folderIndex].files.findIndex(file => file.id === sourceId)
// returning all the indices
return [folderIndex, destinationFolderIndex, fileIndex]
}
// move function
function move(list: Folder[], source: string, destination: string): Folder[] {
// cloning the list
const listToEdit = [...list];

// find all necessary indices with the findIndicesByFileId function
const [sourceFolderIndex, destinationFolderIndex, fileIndex] = findIndicesByFileId(list, source, destination)
// getting the file to move out using Array.Prototype.splice() (which returns the array of deleted elements so, since I am removing only one element the array will always be of size 1)
const fileToMove = listToEdit[sourceFolderIndex].files.splice(fileIndex, 1)[0]
// pushing the file to move into the destination
listToEdit[destinationFolderIndex].files.push(fileToMove)
// returning the updated list
return listToEdit
}

console.log(move(list, "4", "6"))

最新更新