gpt4 book ai didi

JQuery 和 Phonegap 将本地存储中的数据保存在数组中

转载 作者:行者123 更新时间:2023-12-01 05:52:49 24 4
gpt4 key购买 nike

我有存储在本地存储中的数据,我想将其拉入数组。但没有成功...正如您将在下面的代码中看到的,我正在尝试 console.log 数组,但它写给我:

(Chrome 控制台)

Uncaught SyntaxError: Unexpected end of input index.html:204 (anonymous function) index.html:204 e.resolveWith jquery-1.6.min.js:16

e.extend.ready jquery-1.6.min.js:16 c.addEventListener.z jquery-1.6.min.js:16

cordova :: fired deviceready event! ripple.js:37

8 canvas not found!

这是我的本地存储: enter image description here这是代码:

            $(function() {
window.localStorage.setItem('Sample block', '{"time":1383107260539,"counter":5}');

track_items_for_chart = [];

for (i = 0; i < window.localStorage.length; i++) {
var key_name = (window.localStorage).key(i);

var time = (window.localStorage.getItem(key_name));
time = (JSON.parse(time)).time;
var counter = jQuery.parseJSON(window.localStorage.getItem(key_name)).counter;
counter = JSON.parse(counter);
track_items_for_chart.push("{ date: " + time + ", km: " + counter + "},");
}
console.log(track_items_for_chart);

});

最佳答案

这些是我编写的函数,用于将字符串转换为数组、添加数据、转换回字符串,然后保存到本地存储。

// Converts a string from local storage to an array
// to be used by program
function convert_string_to_array (string)
{

var array = string.split("^");
return array;
}

// Converts array to string to be saved into local storage

function convert_array_to_string (array)
{

var string;

for (var i = 0; i < array.length ; i++)
{
if (string != undefined){
string = string + "^" + array[i]; //
}else{
string = array[i];
}
}

return string;
}

// Used to allow user to store a string

function add_to_array(array,welcome) // simple function to add data to array
{
array[array.length] = prompt(welcome);
}

基本上,如果你有一个字符串数组,例如 myStrings = ["Hello","hi","hey how are you"]这将允许您将其存储到本地存储以供将来使用,它将以 localStorage.myStrings = Hello^hi^hey how are you^ 的形式存储。另外,如果您希望 ^ 成为数据中的常见字符,只需将其更改为上述函数中您想要的任何字符即可。

关于JQuery 和 Phonegap 将本地存储中的数据保存在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com