简体   繁体   English

如何在列表中获取数组? 使用 React.JS,Typescript

[英]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:一步一步解释:

  • find the destination folder & source folder找到目标文件夹和源文件夹
  • copy source into destination folder将源复制到目标文件夹
  • filter the source folder from the source从源中过滤源文件夹
     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
}

Playground 操场


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.

相关问题 如何在 React.JS 中正确交换数组元素? - How do I swap array elements correctly in React.JS? 如何在 react.js 中获取容器的偏移坐标? - How do I get the offset coordinates of a container in react.js? 如何让 React.js TypeScript 通过 onKeyDown 事件接受 'event.target' 上的 'parentElement'? - How can I get React.js TypeScript to accept 'parentElement' on 'event.target' with a onKeyDown event? React.js如何从js文件获取数组 - React.js how to get array from js file 我如何使用 moment.js 获取和应用今天的价值?(react.js) - How do i get and apply today value with moment.js?(react.js) 我如何在 react.js 中遍历 object 数组并在 object 中添加价格值,如下所示 - How do i iterate over an array of object in react.js and add the values of prices in the object as shown below React.js:如何从数组中渲染组件中特定类别的数据? - React.js: how do i render a particular category of data in a component from an array? 如何将异步数据放入 React.js 中的全局数组中? - How to get asynchronous data into a global array in React.js? React.js如何使用getElementById方法获取道具中的ID? - React.js How do I get the id in props with the getElementById method? 我如何在 react.js 中使用数组输入 - How can i use array input in react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM