保存数组javascript [英] Save array javascript
问题描述
我有一个像这样的长数组:
I have a long array like this:
var array = [{x: 0, y:0} ...];
谁包含近2000个对象。
如何将其导出到文本并在另一个javascript文件/项目中使用?
Who contains almost 2000 objects. How can I export this to text and use it in another javascript file/project?
推荐答案
使用 JSON.stringify(数组)
。这将返回一个字符串,您可以将其保存在服务器上的某个位置,然后再次将其重新用作javascript数组。
Use JSON.stringify(array)
. This will return you with a string that you can save somewhere, say, on the server, and then re-use it as javascript array again.
将字符串发送到在服务器上,您可以使用 jQuery的$ .ajax 功能。要将其检索回来,您可以再次使用$ .ajax函数,但也可以指定服务器的结果是json类型。为此,请在函数中指定 dataType
为 json
。
To send the string to a server, you can use jQuery's $.ajax function. To retrieve it back, you can again use the $.ajax function, but it might be a good idea to also specify that the result from server is of type json. To do that, specify dataType
to be json
in the function.
这篇关于保存数组javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!