简体   繁体   English

如何在单击按钮时从本地存储中删除多维数组?

[英]How do you delete a multidimentional array from localstorage on button click?

On my table when a value is input, it is stored to localstorage as a 2D array and mapped to a table on a new seperate row.在我的表上,当输入值时,它作为二维数组存储到 localstorage 并映射到新的单独行上的表。 When a new row is created, it also has its own delete button.创建新行时,它也有自己的删除按钮。 When that delete button is clicked, that specific row with the button will be deleted.单击该删除按钮时,将删除带有该按钮的特定行。

an example is [['hello', 'hi', 'hey'], ['bye', 'goodbye', 'cya']] ;一个例子是[['hello', 'hi', 'hey'], ['bye', 'goodbye', 'cya']] ; the first nested array would have the 3 data it contains mapped on one row, and the second nested array on the next row.第一个嵌套数组会将它包含的 3 个数据映射到一行,第二个嵌套数组映射到下一行。 If I wanted to delete the first row, I would click the delete button which would result in the first nested array being removed resulting in the first row being removed.如果我想删除第一行,我会单击删除按钮,这将导致删除第一个嵌套数组,从而删除第一行。

Since the table is based off of localstorage, I will need to delete that specific array but keep the others.由于该表基于本地存储,因此我需要删除该特定数组但保留其他数组。 I think I can achieve this by getting that nested arrays index and removing it.我想我可以通过获取嵌套数组索引并删除它来实现这一点。 Now I am not sure whether I need to use .splice() or another method?.现在我不确定是否需要使用.splice()或其他方法?。 This is the method I have used which has not worked:这是我使用的方法,但没有奏效:

const getData = [JSON.parse(localStorage.getItem('pls'))];

const tableDatas = [...getData];

 const handleSingleDelete = (index) => {
   //what do I place here? I tried this:
   getItem.splice(index, 1)
}
...
return(
    <Table className="tablesorter" responsive id="maintb">

    <tbody id="tbid">
                    {(tableDatas.map((l,i) => (
                      <tr key={`${l+i}`} id={`${i}`}>
                          <td>{tableDatas[i][1]}</td>
                          <td>{tableDatas[i][2]}</td>
                          <td>{tableDatas[i][3]}</td>
                          <td>
                          <Button onClick={handleSingleDelete}>Delete
                          </Button>
                          </td>
                       </tr>
             </tbody>
    <Table>
)

You should get the Array from the localStorage each time you want to make a change in it, because you always need the most recent data, calling it first will always have the same array and any future changes to the localstorage will not be updated in the variable until you call it again.每次要对其进行更改时,都应该从localStorage获取 Array,因为您始终需要最新的数据,首先调用它将始终具有相同的数组,并且以后对 localstorage 的任何更改都不会在变量,直到您再次调用它。

First bring it into the function, then remove the index you want and parse again to save it to the localstorage :首先将其带入函数中,然后删除您想要的索引并再次解析以将其保存到localstorage

const localStorageName = "pls";
const handleSingleDelete = (index) => {
    const currentArray = JSON.parse(localStorage.getItem(localStorageName));
    currentArray.splice(index, 1);
    localStorage.setItem(localStorageName, JSON.stringify(currentArray))
}

Thanks to woohaik and some modification I got the results by doing this:感谢 woohaik 和一些修改,我通过这样做得到了结果:

    const localStorageName = 'pls';
    const [arrData, setArrData] = useState(JSON.parse(localStorage.getItem(localStorageName)))

    function handleSingleDelete(e,i){
      //gets array from localstorage
      //Splices it via index of specific row
      //sets modified array as new array and deletes it from rendered view.
      const currentArray = JSON.parse(localStorage.getItem(localStorageName));
      currentArray.splice(i, 1);
      localStorage.setItem(localStorageName, JSON.stringify(currentArray))
      setArrData(() => currentArray)
    }
...
return(
                    {(tableDatas.map((l,i) => (
                      <tr key={`${l+i}`} id={`${i}`}>
                          {/*<td>{i}</td>*/}
                          <td>{tableDatas[i][2]}</td>
                          <td>{tableDatas[i][0]}</td>
                          <td>{tableDatas[i][3]}</td>
                          <td>{tableDatas[i][1]}</td>
                          <td>Idle</td>
                          <td>
                          <Button onClick={e => handleSingleDelete(e, i)}>Delete
                          </Button>
                          </td>
                      </tr>
...
);

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

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