简体   繁体   中英

Object Assign Nested Property Javascript

I am new to React JS (Javascript) and getting problems during merge the previous records with the new records in Pagination.

Below is the code:-

First Page Result

{
  "status": 200,
  "error": null,
  "message": null,
  "result": {
    "data": [{
      "SuraID": 116,
      "ChapterID": 2,
      "SuraName": "The Cow",
      "Sajda": 0,
      "Ayah": 286,
      "Latin": "Al-Baqara",
      "AyahText": [{
          "id": 6283,
          "text": "O children of Israel, remember the favours I bestowed on you. So keep your pledge to Me, and I will mine to you, and be fearful of Me, ",
          "found": "1"
        },
        {
          "id": 6290,
          "text": "Remember, O children of Israel, the favours I bestowed on you, and made you exalted among the nations of the world. ",
          "found": "1"
        }
      ]
    }],
    "totalFound": 598,
    "prev_page_url": "1",
    "next_page_url": 2,
    "totalPages": 27,
    "percentage": 0.38390180331131357
  }

Second Page Result

{
  "status": 200,
  "error": null,
  "message": null,
  "result": {
    "data": [{
        "SuraID": 116,
        "ChapterID": 2,
        "SuraName": "The bufallow",
        "Sajda": 0,
        "Ayah": 286,
        "Latin": "Al-Baqara",
        "AyahText": [{
            "id": 6283,
            "text": "O children of Israel, remember the favours I bestowed on you. So keep your pledge to Me, and I will mine to you, and be fearful of Me, ",
            "found": "1"
          },
          {
            "id": 6290,
            "text": "Remember, O children of Israel, the favours I bestowed on you, and made you exalted among the nations of the world. ",
            "found": "1"
          }
        ]
      }
      "totalFound": 598,
      "prev_page_url": "2",
      "next_page_url": 3,
      "totalPages": 27,
      "percentage": 0.38390180331131357
    }

Final Result Should like the below JSON

{
  "status": 200,
  "error": null,
  "message": null,
  "result": {
    "data": [{
      "SuraID": 116,
      "ChapterID": 2,
      "SuraName": "The Cow",
      "Sajda": 0,
      "Ayah": 286,
      "Latin": "Al-Baqara",
      "AyahText": [{
          "id": 6283,
          "text": "O children of Israel, remember the favours I bestowed on you. So keep your pledge to Me, and I will mine to you, and be fearful of Me, ",
          "found": "1"
        },
        {
          "id": 6290,
          "text": "Remember, O children of Israel, the favours I bestowed on you, and made you exalted among the nations of the world. ",
          "found": "1"
        }
      ]
    }, {
      "SuraID": 116,
      "ChapterID": 2,
      "SuraName": "The Cow",
      "Sajda": 0,
      "Ayah": 286,
      "Latin": "Al-Baqara",
      "AyahText": [{
          "id": 6283,
          "text": "O children of Israel, remember the favours I bestowed on you. So keep your pledge to Me, and I will mine to you, and be fearful of Me, ",
          "found": "1"
        },
        {
          "id": 6290,
          "text": "Remember, O children of Israel, the favours I bestowed on you, and made you exalted among the nations of the world. ",
          "found": "1"
        }
      ]
    }],
    "totalFound": 598,
    "prev_page_url": "1",
    "next_page_url": 2,
    "totalPages": 27,
    "percentage": 0.38390180331131357
  }

Pseudo code:

let firstPageData = //from 1 fetch and after JSON.parse(data);
let secondPageData = //from 2 fetch and after JSON.parse(data);

let mergedPage= [...firstPageData,...secondPageData ];

or

let  mergedPage= firstPageData.concat(secondPageData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM