【问题标题】:Add indexed array to localStorage将索引数组添加到 localStorage
【发布时间】:2014-10-02 09:41:51
【问题描述】:

如何将这个索引数组 names 添加到 localStorage。并调用这个元素:

var name= new Array();

for( var i=0; i<results.rows.length; i++ ) {
    names[results.rows.item(i).IdTypePrestation ] = results.rows.item(i).LibellePrestation;
}

如果我改变这一行:

names[results.rows.item(i).IdTypePrestation] = results.rows.item(i).LibellePrestation;

到这里:

names[i] = results.rows.item(i).LibellePrestation;

一切正常,但我需要第一行。

【问题讨论】:

    标签: javascript jquery-mobile cordova local-storage


    【解决方案1】:

    设置为localStorage:

    localStorage.someArrayValue = JSON.stringify(myArray);
    

    从localStorage获取:

    myArray = JSON.parse(localStorage.someArrayValue);
    

    【讨论】:

    • localStorage['names']=JSON.stringify(name);
    • @B.M.A:这怎么行不通?它有什么作用,您需要/希望它做什么?
    • 我想将此标签 names 添加到我的本地存储中,但是当我调用这样的元素时`var j=results.rows.item(0).IdTypePrestation; var retrievedObject1 = JSON.parse(localStorage['tableau_type']); alert(retrievedObject1 [j]) 返回未定义
    • 你在localStorage中设置了'tableau_type',在做之前:JSON.parse(localStorage['tableau_type']);?
    【解决方案2】:

    我认为您的解决方案应该如下所示:

    //Set names on tableau_type line of localStorage
    localStorage['tableau_type'] = JSON.stringify(names);
    
    //Get all names on tableau_type line of localStorage
    var retrievedObject = JSON.parse(localStorage['tableau_type']);
    $.each(retrievedObject ,function(index,element){
        alert(element);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2022-06-29
      相关资源
      最近更新 更多