Recreating JSON data (yes...again)

There’s a data.feed.entry array that is set up like so:

entry: (245) [{…}, {…}, {…}, {…},....etc]

That’s taken from my console. So it looks like it’s a regular array, with 245 items inside of it.
If I open that up and look inside, I see this:

0: {id: {…}, updated: {…}, category: Array(1), title: {…}, content: {…}, …}

So it looks like from here, that each of the 245 will be a key/value object array. Below is the expanded version of [0]

0:
gsx$city: {$t: 'asdf'}
gsx$morelikethis: {$t: 'content'}
gsx$morelikethis: {$t: 'content'}
gsx$morelikethis: {$t: 'content'}

There are 12 “keys” listed, but I just kept 4 here for now. I’m trying to recreate this dataset. Here is what I have so far:

$.ajax({
    type: "GET",
    url: "datafile.tsv",
    dataType: "text",
    success: function(tsv) {
      var TSVData = tsv.split("\n").map((x) => x.split("\t"));
      console.log(TSVData); 
      var data = {
        feed: {
          entry: [
            {}
          ]
        }
      };
      for(var i = 0; i < 1; i++) {
        for(var j = 0; j < TSVData[i].length; j++) {
          var fdsa = "gsx$"+TSVData[i][j].toLowerCase().split(" ").join("");
          data.feed.entry[i].fdsa = "test";
          // data.feed.entry[i] += "<td>"+TSVData[i][j]+"</td>";
        }
      }
      console.log(data.feed);
    }
  }); 

I’m just having trouble figuring out how I can properly assign keys in my data set. In the original dataset, I can console log something like data.feed.entry[i].gsx$schoolid.$t. So I’m trying to figure out how I can (for now) call something like data.feed.entry[0].gsx$schoolid. I’ve tried the above and also:

data.feed.entry[i].gsx$+TSVData[i][j].toLowerCase().split(" ").join("") = "test";

I’m sure I’m missing something very obvious but I’m missing it.

Got it! The old data has 242 arrays in it, the new data has 243, but the first array are the headers, so I adjust my initial for loop to start at 1 and luckily the old data follows a specific naming convention so I was able to replicate the data perfectly and get this treatment fixed!!

var TSVData = tsv.split("\n").map((x) => x.split("\t"));
      var data = {
        feed: {
          entry: []
        }
      };
      for(var i = 1; i < TSVData.length; i++) {
        var placeholderData = {};
        for(var j = 0; j < TSVData[i].length; j++) {
          placeholderData["gsx$"+$.trim(TSVData[0][j].toLowerCase().split(" ").join(""))] = {$t: TSVData[i][j]};
        }
        data.feed.entry.push(placeholderData);
      }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.