单击方法后从localStorage删除项目

时间:2020-09-11 21:13:18

标签: javascript

我正在尝试从保存在apply plugin: 'com.android.application' android { compileSdkVersion 28 buildToolsVersion "30.0.2" defaultConfig { applicationId "com.example.mltest" minSdkVersion 28 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } aaptOptions { noCompress "tflite" noCompress "lite" } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly' implementation 'org.tensorflow:tensorflow-lite-task-text:0.0.0-nightly' implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } 中的待办事项列表中删除项目。

我正在考虑做类似的事情:

localStorage

在这部分代码中:

 let deleteTodos = () => {localStorage.removeItem(todos["hola"]); };

这是给您的完整代码以及整个应用的构想

list.addEventListener("click", (e) => {
  if (e.target.classList.contains("delete")) {
    e.target.parentElement.remove();
    localStorage.setItem("todos", JSON.stringify(todos));
  }
});

1 个答案:

答案 0 :(得分:0)

如果要将字符串化的待办事项保存在“ todos”下的localStorage中,则无法通过localStorage.removeItem(todos["hola"])删除单个项目。

在不评论您是否应该以这种方式使用localStorage的情况下,要删除一项,您需要替换localStorage中的整个对象:

const deleteTodo = (label) => {
  // get the items from storage
  const oldTodos = JSON.parse(localStorage.getItem('todos'));

  // delete the specified item. (however you need to do it. this might not work with your specific data structure)
  const newTodos = oldTodos.filter(({ label: x }) => x === label);

  // re-reserialize to local storage
  localStorage.setItem('todos', JSON.stringify(newTodos));
};