-1

I have a big Array with 100.000 objects in JSON in it. Since I need to have certain key/values of those objects grouped together in different arrays I created new arrays with just a couple of key/values from the original array.

Just an example array:

var data = [
{
    "ID": 1,
    "Completed": "yes",
    "Prio": 3,
    "Importance": 2
  },
  {
    "ID": 2,
    "Completed": "yes",
    "Prio": 4,
    "Importance": 4
  }
]

Creating one new Array:

var newArray = [];

for (var i = 0; i < data.length; i++) {

    newArray[i] = {"ID": data[i].ID, "Prio": data[i].Prio};

}

Now it would be nice if I could have separate files, each with an array containing different key/values pairs from the original one (the "data" array).

How can I get those new arrays to in new files? Or how can I at least let me output all objects with each element in them to copy and past that in a new file?

Could JSON help me there?

Unknown User
  • 505
  • 1
  • 7
  • 19
  • Are you doing this in the browser or server/local? – Jordan Running May 30 '17 at 20:52
  • @JordanRunning Just in the browser. – Unknown User May 30 '17 at 20:53
  • What does "separate files" mean, then? Do you want to trigger *n* simultaneous downloads in the user's browser? Or do you want to present the user with a list with a link to download each file? Please be specific about what the desired solution looks like. – Jordan Running May 30 '17 at 20:57
  • I think it's worth noting that it looks like OP is starting to think about ways to be more efficient with data storage (a great and exciting time for computer scientists!), which segues into my point: in industry and academia this problem is typically handled using a database which will typically use complex [search trees](https://en.wikipedia.org/wiki/Search_tree) to store data – Patrick Barr May 30 '17 at 20:57
  • @JordanRunning With separate files I mean that I need to externalise certain key/values from the original array in new arrays that are in individual files so I can load them if I want. Since a) I dont necessairily need all key/value pairs and b) Need less complex elements in the array to analyse the data. I don't want to trigger user downloads. I want to create a data viz. – Unknown User May 30 '17 at 21:03
  • You certainly won't be able to do this without client server communication. – Bernd Strehl May 30 '17 at 21:06

1 Answers1

0

You can use Node fs if you're doing this server side, to write a file. If you do this on the client side, you would have to make an ajax request to your server, to save the submitted arrays in a file.

Edit: If you want to create a file and save it on the client side you could use a library like FileSaver.js

Also take a look at this question

Bernd Strehl
  • 2,852
  • 4
  • 24
  • 43