BeerusDev
BeerusDev

Reputation: 1509

Append JSON Nested Object

I recently had to change the field type of a column which changed the layout of the data as it is now a multiple choice field type.

The original JSON that was pulled through fetch was like so:

d: {
  "results": [
    {
      "MajorTasks": null,
      "DeliverablesSubmitted": null,
      "PersonnelActions": null,
      "Upcoming": "\r\n  <div>None at this time</div>\r\n",
      "Training": null,
      "ResourceRequest": "\r\n  <div>None at this time.</div>\r\n",
      "SupportRequest": "\r\n  <div>None at this time.</div>\r\n",
      "Team": "AMMO",
      },

The new JSON that is now being pulled through, nests the Team column:

d: {
  "results": [
    {
      "MajorTasks": null,
      "DeliverablesSubmitted": null,
      "PersonnelActions": null,
      "Upcoming": "\r\n  <div>None at this time</div>\r\n",
      "Training": null,
      "ResourceRequest": "\r\n  <div>None at this time.</div>\r\n",
      "SupportRequest": "\r\n  <div>None at this time.</div>\r\n",
      "Team": {
        "__metadata": {
          "type": "Collection(Edm.String)"
        },
        "results": [
          "AMMO"
        ]
      },

I noticed it changed when everything was appending except for the Team item, and when it appended it was showing [object Object].

If everything is appending through data[i].valueName except for Team, whats the proper Syntax I guess.

Here is my JS:

function loadData(url) {
    url = partUrl + "/_api/web/lists/getbytitle('WeeklyReport')/items?$select=DeliverablesSubmitted,MajorTasks,PersonnelActions,SupportRequest,ResourceRequest,Team/Value,Training,Upcoming,WeekOf,TravelODC";
    return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request
      .then((r) => {
        if (!r.ok) throw new Error("Failed: " + url);  // Check for errors
        return r.json();  // parse JSON
      })
      .then((data) => data.d.results);
  }
  loadData()
    .then((results) => {
        const data = results;
        var listContent = '';
      
      for (var i = 0; i < data.length; i++) {
          var currData = data[i];
         listContent += '<li data-weekOf="'+ currData.WeekOf+'">';
         listContent += '<h2>' + currData.Team  +'</h2>';
         listContent += '<h4> Tasks </h4>';
         if(currData.MajorTasks !== null){
            listContent += '<ul>' + "- " + currData.MajorTasks.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
              listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Deliverables Submitted</h4>';
                 if(currData.DeliverablesSubmitted !== null){
         listContent += '<ul>' + "- " + currData.DeliverablesSubmitted.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Personnel Actions </h4>';
                 if(currData.PersonnelActions !== null){
         listContent += '<ul>' + "- " + currData.PersonnelActions.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Upcoming Events </h4>';
                 if(currData.Upcoming !== null){
         listContent += '<ul>' + "- " + currData.Upcoming.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Training </h4>';
         if(currData.Training !== null){
                 listContent += '<ul>' + "- " + currData.Training.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Resource Request </h4>';
         if(currData.ResourceRequest !== null){
         listContent += '<ul>' + "- " + currData.ResourceRequest.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Support Request </h4>';
         if(currData.SupportRequest !== null){
         listContent += '<ul>' + "- " + currData.SupportRequest.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '<h4> Travel/ODCs </h4>';
         if(currData.TravelODC !== null){
         listContent += '<ul>' + "- " + currData.TravelODC.replace(/(<([^>]+)>)/gi, "") + '</ul>';
         }else{
         listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
         }
         listContent += '</li>';
 }
   $('#report-summary').html(listContent);
   $('#under_txt').text(' ');
  });
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $('#under_txt').text(value);
    $('li').fadeOut(10);
    $('[data-weekOf='+value+']').fadeIn();
  });
  
});
function sortNewestFirst(){
  var elements = $('[data-weekOf]');
  elements.sort(function (a, b) {
      return new Date($(b).attr('data-weekOf')) - new Date($(a).attr('data-weekOf'));
  });
  $('#report-summary').html(elements);
 }
function sortOldestFirst(){
  var elements = $('[data-weekOf]');
  elements.sort(function (a, b) {
      return new Date($(a).attr('data-weekOf')) - new Date($(b).attr('data-weekOf'));
  });
  $('#report-summary').html(elements);
  }
var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#report-summary').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('weeklyreport.pdf');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Upvotes: 1

Views: 226

Answers (2)

BeerusDev
BeerusDev

Reputation: 1509

What I did to fix this was let:

for (var i = 0; i < data.length; i++) {
  var currData = data[i];
  listContent += '<li data-weekOf="'+ currData.WeekOf+'">';
    if(currData.Team !== null){
      listContent += '<h2>' + currData.Team.results.join(', ') +'</h2>';
    }else{
      listContent += '<h2>' + "Null" +'</h2>';
    }

Upvotes: 0

Gal Gur-Arie
Gal Gur-Arie

Reputation: 394

in this case:

data[i].Team.results[0]

Upvotes: 1

Related Questions