Home > Enterprise >  React Native Display Nested Array Items from API using Flatlist
React Native Display Nested Array Items from API using Flatlist

Time:04-19

I am trying to fetch and display the data from API. I want to show the value of "Timestamp" from "dates" and "modules" array. Kindly help me to resolve this

[
    {
            "id": 8,
            "name": "Topic 1",
            "modules": [
                {
                    "id": 3,
                    "name": "Tugas 1",
                    "dates": [
                        {
                            "label": "Due:",
                            "timestamp": 1650301200
                        }
                    ]
                }
            ]
        },
        {
            "id": 9,
            "name": "Topic 2",
            "modules": [
                {
                    "id": 4,
                    "dates": [
                        {
                            "label": "Opened:",
                            "timestamp": 1649696400
                        },
                        {
                            "label": "Due:",
                            "timestamp": 1651165200
                        }
                    ]
                }
            ]
        }
 ]

CodePudding user response:

Hello Welcome to stack overflow. I prettified your results. I'm not pretty sure of this code but just try this.

console.log(res.data[0].modules[0].date[0].timestamp) 

    [{
  "id": 8,
  "name": "Topic 1",
  "visible": 1,
  "summary": "",
  "summaryformat": 1,
  "section": 1,
  "hiddenbynumsections": 0,
  "uservisible": true,
  "modules": [{
    "id": 3,
    "url": "https://angkasa.rg.telkomuniversity.ac.id/pedagogical/mod/assign/view.php?id=3",
    "name": "Tugas 1",
    "instance": 1,
    "contextid": 49,
    "visible": 1,
    "uservisible": true,
    "visibleoncoursepage": 1,
    "modicon": "https://angkasa.rg.telkomuniversity.ac.id/pedagogical/theme/image.php/boost/assign/1649055720/icon",
    "modname": "assign",
    "modplural": "Assignments",
    "indent": 0,
    "onclick": "",
    "afterlink": null,
    "customdata": "{"
    duedate ":1650301200}",
    "noviewlink": false,
    "completion": 1,
    "completiondata": {
      "state": 1,
      "timecompleted": 1649746942,
      "overrideby": null,
      "valueused": false,
      "hascompletion": true,
      "isautomatic": false,
      "istrackeduser": true,
      "uservisible": true,
      "details": []
    },
    "dates": [{
      "label": "Due:",
      "timestamp": 1650301200
    }]
  }]
},
}]

CodePudding user response:

data.forEach 
((e)=>{
e.modules.forEach((module)=>{
module.dates.forEach{
((date)=>{console.log(date.timestamp);
});
});
});
  • Related