本文介绍了环路放大器;通过localStorage的所有物品逛逛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过localStorage的尝试循环打通 localStorage.length 所有项目,与我的搜索算法的工作。如果我改变: I< localStorage.length 里面的for循环只是一个数字,例如:为(i = 0; I< 100;我++)而不是:(i = 0; I< = localStorage.length-1;我++)全部的寄托的作品。不过,我意识到问题可能在于搜索算法。

I'm trying to loop through localStorage to get ALL items through localStorage.length that works with my search algorithm. If i change: i < localStorage.length inside the for loop to simply a number, i.e: for (i=0; i<100; i++) instead of: (i=0; i<=localStorage.length-1; i++), everthing works. However, I do realize the problem might lie in the search algorithm.

在code让所有项目:

The code getting all items:

   var name = new Array();

   for (var i = 0; i <= localStorage.length - 1; i++) { // i < 100 works perfectly
   key = localStorage.key(i);
   val = localStorage.getItem(key); 
   value = val.split(","); //splitting string inside array to get name
   name[i] = value[1]; // getting name from split string
   }

我的工作搜索算法(!?):

My working (!?) search algorithm:

 if (str.length == 0) { 
  document.getElementById("searchResult").innerHTML = "";
  }   
  else {          
      if(str.length > 0) {
          var hint = "";
          for(var i=0; i < name.length; i++) {                
                if(str.toLowerCase() == (name[i].substr(0, str.length)).toLowerCase()) { //not sure about this line
                    if(hint == "") {                            
                            hint = name[i];                         
                        } else {                            
                            hint = hint + " <br /> " + name[i];                                 
                        }                 
                   }                      
             }            
       }          
}

 if(hint == "") {   
document.getElementById("searchResult").innerHTML=str + " står inte på listan";     
} else {        
    document.getElementById("searchResult").innerHTML = hint;       
    }
 }

什么是错我的 localStorage.length ,或什么是错的搜索算法?

What is wrong with my localStorage.length, or what is wrong with the search algorithm?

推荐答案

现在的问题解决了。问题是,每一次数据被保存到本地存储,一个额外的空项是存储在本地数据库作为一个for循环写错误的后果底部arrayIndex&LT(在setItem部分); guestData.length应该已经arrayIndex&LT; guestData.length-1。 arrayIndex&LT; guestData.length-1存储所有项目,而不在后来搞砸搜索算法的数据库的底部创建一个空的项目,作为最后的价值被搜索是未定义(空项)。

Problem now SOLVED. The issue was that each time data was saved to localStorage, one extra empty item was stored at the bottom of the local db as a consequence of an incorrectly written for loop (in the setItem part.) arrayIndex < guestData.length should have been arrayIndex < guestData.length-1. arrayIndex < guestData.length-1 stores all items without creating an empty item at the bottom of the database which later messed up the search algorithm, as the last value to be search was undefined (the empty item).

这篇关于环路放大器;通过localStorage的所有物品逛逛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:35