-1

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?

Hiero
  • 2,182
  • 7
  • 28
  • 47

3 Answers3

8

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";
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
3

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.

leopik
  • 2,323
  • 2
  • 17
  • 29
  • 1
    `This will return you with a string that you can save somewhere, say, on the server` this is the important part, how can he do that? – Rory McCrossan Apr 09 '15 at 09:23
  • "and then re-use it as javascript array": how? Perhaps edit your question to mention that. – Andy Apr 09 '15 at 09:23
0

Stringify it and copy it to another JavaScript project

JSON.stringify(array);
Vigneswaran Marimuthu
  • 2,452
  • 13
  • 16