javascript - 在 React 中删除另一个对象中的一个对象

标签 javascript reactjs

首先,我知道 React 是 javascript,但这并不是我能做到这一点的原因。遵循“纯函数”的哲学,我使用过滤器来删除东西。

问题是这样的:我有一个集合,其中每个集合都有一个标题和一个包含其他对象的数组,而我想要做的是删除主对象数组中的特定对象。

示例:

// the state is like this
const collections = [ 
{
    id: 3,
    title: 'Machine Learning repos',
    coll_type: 'r',
    url: 'http://127.0.0.1:8000/api/collections/3/',
    repos: [
      {
        id: 68,
        name: 'tensorflow',
        owner: 'tensorflow',
        collection: 3
      },
      {
        id: 76,
        name: 'requests',
        owner: 'kennethreitz',
        collection: 3
      }
    ]
  }
]

当我单击要删除的按钮时,它会调用此函数,并传递集合的 id 和存储库:

const handleRepoDelete = (collectionId, repoId) => {
    // get the collections state
    const _newCollections = [...collections];
    // get the collection by id
    const collection = _newCollections.filter(
        _collection => {
            return _collection.id === collectionId
        }
    )[0]

    // remove repo by id
    collection.repos.filter(
        repo => {
            // here is not works
            // is returning even when the ids are different
            return repo.id !== repoId
        }
    )

    // remove old collection with repo to delete
    _newCollections.filter(
        _collection => {
            // here alse is not works
            // is returning even when the ids are different, and the new and old collection
            return _collection.id !== collection.id
        }
    )
    // iterate in array with old collection deleted
    // and add new collection with repo deleted
    const newCollections = [..._newCollections, collection];
    setCollections(newCollections);
}

问题是我不知道解决此问题的最干净方法,并且如您所见,单击的存储库并未在过滤方法中删除。

最佳答案

这是一个可能的解决方案。

const handleRepoDelete = (collectionId, repoId) => {
  // we will map each item of the collections array
  // will only change the collection with collectionId
  // meaning, if not, we will return the original collection
  const newCollections = collections.map(
    (collection) => {
      const {
        id, repos
      } = collection
      // if the id doesn't match, just return it
      if (id !== collectionId) {
        return collection
      }
      // otherwise here we return a new object
      // only changing repos prop of it
      // using spread syntax, we filter repos
      // at the end, we returned the collection
      // with the changes we want
      return {
        ...collection,
        repos: repos.filter( ({ id }) => id !== repoId )
      }
    }
  )
  setCollections(newCollections);
}

关于javascript - 在 React 中删除另一个对象中的一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570920/

相关文章:

reactjs - 在 react redux 中的全局状态更改后调度新 Action

javascript - "position: fixed"与包含 svg 元素的 div 一起使用时会导致许多问题

javascript - 无法使用 Protractor 正确执行页面对象模式

javascript - 如何自动调用 RESTful Web 服务

javascript - 如何使用 RequireJS 加载模块导入 Flow 的类型信息?

javascript - React 什么时候开始支持 jsx 中的 'class' 属性

javascript - express : Specify HTTP status code when throwing error in service

javascript - 在 ES6 Node.js 中导入 '.json' 扩展会引发错误

reactjs - 记住 JSX 元素以便稍后将它们通过 props 传递给拥有的组件是否安全?

javascript - React 从表中删除列