I have a long array like this:
var array = [{x: 0, y:0} ...];
Who contains almost 2000 objects. How can I export this to text and use it in another javascript file/project?
I have a long array like this:
var array = [{x: 0, y:0} ...];
Who contains almost 2000 objects. How can I export this to text and use it in another javascript file/project?
Try like this
var array = [{
x: 0,
y: 0
}];
var a = document.body.appendChild(
document.createElement("a")
);
a.download = "export.txt";
a.href = "data:text/plain;base64," + btoa(JSON.stringify(array));
a.innerHTML = "download example text";
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.
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.
Stringify it and copy it to another JavaScript project
JSON.stringify(array);