简体   繁体   English

在本地存储中存储数组

[英]Storing arrays in localstorage

I'm building an app with Appery.io.我正在使用 Appery.io 构建一个应用程序。 It's basically a search app that returns results and then you can click into the results to see details.它基本上是一个返回结果的搜索应用程序,然后您可以单击进入结果以查看详细信息。

Results are returned from a REST api in JSON format, and each results has a number of different items in it, including one that is a JSON array which contains multiple sets of three name/value pairs.结果以 JSON 格式从 REST api 返回,每个结果中有许多不同的项目,其中一个是 JSON 数组,其中包含多组三个名称/值对。

I need the array associated with each result to be available in the details page for that result.我需要与每个结果关联的数组在该结果的详细信息页面中可用。 I don't want to simply run the search again in the details page, because it is a relatively lengthy process and would slow down the entire app.我不想简单地在详细信息页面中再次运行搜索,因为这是一个相对漫长的过程并且会减慢整个应用程序的速度。

I'm aware of using JSON.stringify() to make the array a string, and then storing each array in local storage.我知道使用JSON.stringify()使数组成为字符串,然后将每个数组存储在本地存储中。 However, when I do that in Appery.io, it seems that I either a) just save the first result of the array, or b) am doing it wrong.但是,当我在 Appery.io 中这样做时,似乎我要么 a) 只保存数组的第一个结果,要么 b) 做错了。

Can any provide any insight into how I can go about doing this?任何人都可以提供有关我如何去做这件事的任何见解吗? Happy to provide code, clarify my question, etc.很高兴提供代码,澄清我的问题等。

Thanks!谢谢!

If there's some problem using JSON.stringify() with Appery.io, try the approach storing them as comma-separated string (unless you're having commas in values):如果在JSON.stringify()使用JSON.stringify()时出现问题,请尝试将它们存储为逗号分隔字符串的方法(除非您的值中有逗号):

> ["foo", "bar"].join();
>> "foo,bar"

And them returning them:他们还给他们:

> "foo,bar".split(",");
>> ["foo, "bar"]

Of course, if you're having commas in values, you can use explicitly specified separator for values, like:当然,如果值中有逗号,则可以使用明确指定的值分隔符,例如:

> ["foo,bar", "foz,baz"].join("#");
>> "foo,bar#foz,baz"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM