如何使用索引检查项目是否在本地存储中(特殊情况)

时间:2019-01-16 05:13:27

标签: javascript ecmascript-6 local-storage

我试图找出解决方法,我试图将一些html代码存储到html字符串代码数组中(如果两个id都匹配),但事实证明,在调试之后,我已经得到的东西与我原本想得到的东西完全不同,这就是我的意思。

问题:

现在进行了一点调试之后,我不确定在本地存储API中获取密钥时,它是否获取了实际密钥以及返回该密钥索引的位置,或者我真的不知道本地存储API如何正常工作。如果我说的没有意义,请查看输出(预期与实际)。

注意:我还将密钥存储到本地存储中的方法是,为列表中的每个项目指定一个ID,从“ 0”开始,一直递增1从API收到的数据的结尾。例如,如果我得到9个项目,那么所有这些项目的ID都应从0一直到第9个项目。

源代码:

import * as World from 'fusioncharts/maps/fusioncharts.world';

输出(错误)

let not_favored = [...document.querySelectorAll(".not-favored")];

for(let index =0; index<not_favored.length;index++ ){


   // Allow type cast instead of doing it manually, recall '==' allows type casting while '===' doesn't
   console.log("the index of the children is " + not_favored[index].children[0].children[0].children[0].children[0].id + " and the index of the local_storage is " + localStorage.key(index+"") )
   if(not_favored[index].children[0].children[0].children[0].children[0].id == localStorage.key(index +"") && localStorage.key(index+"") !== null){
       console.log("how many times this occurs? Does it even work or compare key, which is the ID with the right index of that ID?")
       not_favored[index] = localStorage.getItem(localStorage.key(index.toString()))

    }
    else{
       not_favored[index] = not_favored[index].outerHTML;
     }

预期输出

the index of the children is 0 and the index of the local_storage is 2
the index of the children is 1 and the index of the local_storage is 3
the index of the children is 2 and the index of the local_storage is null
the index of the children is 3 and the index of the local_storage is null
the index of the children is 4 and the index of the local_storage is null

0 个答案:

没有答案