1

I have an array containing 100.000 objects. Those objects have multiple key/value pairs.

Example of my array:

var data = [
{
    "name": "John",
    "id": 1,
    "completed": "yes",
    "prio": 3,
    "importance": 2
  },
  {
    "name": "Jim",
    "id": 2,
    "completed": "yes",
    "prio": 4,
    "importance": 4
  }
]

As shown below I created a new array from the existing one but now with selected key/value pairs from the original array.

var newData = [];

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

    newData[i] = {"name": data[i].name, "id": data[i].id};

    console.log(newData[1]);

}

As you can also see I logged the new array to the console. But what I actually need is to store the array newData in a separate file in which I can see all the objects of the array like in my example array data. To be able to see all the objects of newData is actually not that important but would be nice for working with the array later on. For example I'd like to use R for better understanding of the data.

How can I do that?

NOTE: I'm in the browser!

I'm really struggling with that problem and would be glad if someone could help me …

Thanks!

Unknown User
  • 505
  • 1
  • 7
  • 19
  • 1
    Are you in the browser or Node.js? – Jordan Running May 31 '17 at 20:55
  • @JordanRunning I'm in the browser. – Unknown User May 31 '17 at 20:57
  • 1
    Maybe something like [this](https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server)? – Nelewout May 31 '17 at 20:58
  • 1
    newData = data.map(i => ({name: i.name, id: i.id})); – avrahamcool May 31 '17 at 21:00
  • I am not sure what you are actually tryin to achieve? Is this for debugging? Are you trying to have a hosted file with static json? If you are "in the browser" what could you want with a file? What is your end goal here... – Igor May 31 '17 at 21:01
  • My end goal is to create a data viz. For this I first want to break down my huge array in different arrays. – Unknown User May 31 '17 at 21:02
  • 1
    What's the point of creating new objects that just have some properties from the original objects? Just use the original objects. It would make more sense if you were selecting specific objects in the new array. – Barmar May 31 '17 at 21:28

2 Answers2

2

You could use URL.createObjectURL() along with Blob like so:

var TxtFile = function(text) {

  let data = new Blob([text], {
      type: 'text/plain'
    }),
    file = window.URL.createObjectURL(data);

  // revoke the URL after you're done using it.
  function _revokeObjectUrl() {
    window.URL.revokeObjectURL(textFile);
  }

  return {
    file: file,
    destroy: _revokeObjectUrl
  };
};

const data = [{
    "name": "John",
    "id": 1,
    "completed": "yes",
    "prio": 3,
    "importance": 2
  },
  {
    "name": "Jim",
    "id": 2,
    "completed": "yes",
    "prio": 4,
    "importance": 4
  }
];

const newData = data.map(o => ({
  name: o.name,
  id: o.id
}));

document.querySelector('a').href = new TxtFile(JSON.stringify(newData)).file;

//You can still access the array.
console.log(newData);
<a download>Download file</a>
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
0

You can use a blob to store your data and use this library to download your file on your device:

  var blob = new Blob([JSON.stringify(newData, null, 2)], {type: "text/plain;charset=utf-8"});
  saveAs(blob, filename+".json");