简体   繁体   English

无法从数组中删除对象,react.js

[英]Unable to remove objects from array , react.js

I'm trying to remove an item from a childArray that is nested into another Array.我正在尝试从嵌套到另一个数组中的childArray中删除一个项目。

This is How I'm trying.这就是我正在尝试的方式。

const childArrayHandler = (childData, sub, questionId, data, btnId) => {
    // Manage color change on click
    const isInList = selectedBtnList.some((item) => item === btnId)
    if (isInList) {
      onSelectedBtnListChange(selectedBtnList.filter((item) => item !== btnId))
    } else {
      onSelectedBtnListChange([...selectedBtnList, btnId])
    }

    // Manage childData Array 
    // copy data to mutable object
    const currentChildData = [...childData]
    const hasId = currentChildData.find(({ id }) => sub.id === id)

    if (!hasId) {
      // add item to childArray if same index is not available in the childArray
      const newChild = { id: sub.id, sub_question: sub.sub_question, weightage: sub.weightage }
      currentChildData.push(newChild)
      setChildDataOnChange((current) => [...current, newChild])
    } else if (hasId) {
      // remove item from childArray if same index is available in the childArray
      const indexOfChild = currentChildData.indexOf(hasId)
      // console.log('currentChildData', currentChildData, 'indexOfChild', indexOfChild)
      currentChildData.slice(indexOfChild, 1)
      setChildDataOnChange(currentChildData)
    }

    const newData = [...data]

    // find parent of the child
    const parent = newData.find(({ parentId }) => questionId === parentId)

    // find index of parent
    const indexOfParent = newData.indexOf(parent)
    // update data with child related to parent
    newData[indexOfParent].child = currentChildData
    onDataChange(newData)
    localStorage.setItem('deviceReport', JSON.stringify(newData))
  }

The problem is in the else if block, I want if there's an index of object available in the child then it should remove it from the array.问题出在else if块中,我想如果子项中有可用的索引 object,那么它应该将其从数组中删除。 As far as I can see I'm using the correct approach as other articles suggested to handle but missing something which I cant see right now.据我所知,我使用的是其他文章建议处理的正确方法,但缺少一些我现在看不到的东西。 But not able to find the proper result.但无法找到正确的结果。 If I console it doesn't change anything.如果我安慰它不会改变任何东西。 means don't remove any item from the array if the index is already is there.意味着如果索引已经存在,则不要从数组中删除任何项目。

So how can I fix this, or what might be doing wrong, is there any other way to do it please also mention it?那么我该如何解决这个问题,或者可能做错了什么,有没有其他方法可以解决这个问题,请同时提及? Thanks谢谢

You have the problem with this slice你有这个切片的问题

currentChildData.slice(indexOfChild, 1)

It does not initialize a new array for you ( React's immutability )它不会为你初始化一个新数组( React 的不变性

The fix could be修复可能是

currentChildData = currentChildData.slice(0, indexOfChild).concat(currentChildData.slice(indexOfChild + 1))

If you feel it's too complicating, you can use filter instead如果觉得太复杂,可以用filter代替

currentChildData = currentChildData.filter((item) => item !== hasId) //`hasId` is your found item with `find`

The 2nd problem here is这里的第二个问题是

newData[indexOfParent].child = currentChildData

You cannot assign a value to a mutate object您不能为变异 object 赋值

The proper way should be正确的方法应该是

newData = newData.map((item) => item === parent ? {...item, child: currentChildData} : item)

The full code完整代码

const childArrayHandler = (childData, sub, questionId, data, btnId) => {
    // Manage color change on click
    const isInList = selectedBtnList.some((item) => item === btnId)
    if (isInList) {
      onSelectedBtnListChange(selectedBtnList.filter((item) => item !== btnId))
    } else {
      onSelectedBtnListChange([...selectedBtnList, btnId])
    }

    // Manage childData Array 
    // copy data to mutable object
    let currentChildData = [...childData]
    const hasId = currentChildData.find(({ id }) => sub.id === id)

    if (!hasId) {
      // add item to childArray if same index is not available in the childArray
      const newChild = { id: sub.id, sub_question: sub.sub_question, weightage: sub.weightage }
      currentChildData.push(newChild)
      setChildDataOnChange((current) => [...current, newChild])
    } else  {
      // console.log('currentChildData', currentChildData, 'indexOfChild', indexOfChild)
      currentChildData = currentChildData.filter((item) => item !== hasId)
      setChildDataOnChange(currentChildData)
    }

    //let newData = [...data]

    // find parent of the child
    //const parent = newData.find(({ parentId }) => questionId === parentId)

    // update data with child related to parent
    //newData = newData.map((item) => item === parent ? {...item, child: currentChildData} : item)

    //shorter version
    const newData = data.map((item) => item.parentId === questionId ? {...item, child: currentChildData} : item)
    
    onDataChange(newData)
    localStorage.setItem('deviceReport', JSON.stringify(newData))
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM