jasonmerino / react-native-simple-store

A minimalistic wrapper around React Native's AsyncStorage.

Home Page:https://www.npmjs.com/package/react-native-simple-store

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I want to delete an object in an array

opened this issue · comments

Nice to meet you.
I am not good at English, so the sentences may be strange. I'm sorry.

I am creating a todo application for ios with react-native.
In order to store data in local storage, I would like to configure the store as follows. ↓

Index [
  Item1 {
    Todo: "task",
    Checkbox: false
  },
  Item2 {
    Todo: "task",
    Checkbox: false
  }
]

"Index" and "item" are keys used in push and delete.
"Item" is a random string.

I execute delete(index) when deleting all tasks. Since all task objects are in the index array, if index is specified as an argument of delete method, all tasks are deleted from local storage.

The problem is when deleting one particular task.
There is a delete button in each task, and when you tap it, the task is deleted. I have created a method for deleting a specific task. ↓

DeleteTodo (id) {// id is the key of the task to which the delete button is tapped
    ls.keys ()
    .then (key => {
      key.map (key => {
        If (key === id) {... *1
          ls.delete (key)
        }
      })
    })
    ls.get ('index') ... *2
    .then (list => {
      console.log (list) ... *3
    })
  }

In the part of *1, "If the key of the task tapped in all keys exists, the task is deleted" is executed.
*3 checks whether the task was properly deleted.
Since all the tasks should be present in the index key, index is specified for the get method of *2.

However, console shows tasks that should have been deleted. The tapped task has not been deleted. Try to get all the keys with the key method and check the console.log, the key of the tapped task was not displayed. In other words, only the key of the tapped task is deleted, and the task itself is not deleted.

How can I delete a specific task object that exists in an array?
What is wrong with the method I created?

Thank you for creating react-native-simple-store.

@uneco185 in order to delete a specific task in an array you would need to get the array from the simple store, get the index of that item in the array and then use Array's .splice function to adjust the array and then save it back to the store. That would look something like this:

import store from 'react-native-simple-store';

store
  .get('index')
  .then(arr => {
    let index = null;
    arr.forEach((item, i) => {
      if (/* check if item is the item you're looking for */) {
        index = i;
      }
    });
    if (index !== null) {
      arr.splice(index, 1);
      store.save('index', arr);
    }
  })

I have an issue to add a .splice function to the simple store for saved array values, but I am yet to get around to it. That issue is #32. I hope this code example can help in the mean time until I can get that implemented.

To your second question, I'm still a little unclear about what you are trying to delete when you are getting all the keys and then trying to delete an item based on the keys returned from the store. The .keys function returns all the keys that you save data under (basically the named reference to where you store values). I could be wrong about this, but it seems like you are expecting a relationship between an item you saved to the store at the root level and an item that you saved to that array. The simple store does not store relational data, it just provides an ease-of-use wrapper around AsyncStorage, which I believe under the hood is itself a document store, and not relational either.

@jasonmerino Hello. Thank you for your reply.

A few weeks after I wrote this issue, this problem was solved.
However, as it was a few months ago, I do not remember how to solve the problem... I'm sorry.

TodoApp has been successfully completed and it works without problems. ↓
https://expo.io/@uneco/TodoApp

I am pleased that you kindly reply.
Thank you very much.