Skip to content

1.3.1

Latest

Choose a tag to compare

@heshansw heshansw released this 08 May 14:15
· 1 commit to main since this release

Reorder Nested Object Array (Available with v1.3.0)

Now you can reorder nested object array on ASC or DESC order and return reordered object in same structure as the original object

  • reOrder Reorder nested object array

    • Parameters

      1. originalObject Original Object
      2. keyPath Nested Key Path to be reordered
      3. SortOrder Sorting Order (SortOrder.ASC for Ascending and SortOrder.DESC for descending)

      Lets say you have this data set

      {
          "schoolName":"Doe School",
          "address":"Stockholm",
          "classes":[
              {
                  "className":"Class 01",
                  "year":"Year 01",
                  "studentData":{
                      "students":[
                      {
                          "studentName":"Philip Larry",
                          "age":16
                      },
                      {
                          "studentName":"John Doe",
                          "age":18
                      },
                      {
                          "studentName":"Alex Numan",
                          "age":15
                      },
                      {
                          "studentName":"Joe Max",
                          "age":20
                      },
                      {
                          "studentName":"Remy Max",
                          "age":21
                      }
                      ]
                  }
              },
              {
                  "className":"Class 02",
                  "year":"Year 01",
                  "studentData":{
                      "students":[
                      {
                          "studentName":"Bale Larry",
                          "age":16
                      },
                      {
                          "studentName":"John Doe",
                          "age":18
                      },
                      {
                          "studentName":"Andersson",
                          "age":15
                      },
                      {
                          "studentName":"Joe Max",
                          "age":20
                      }
                      ]
                  }
              }
          ]
          }

      You can get reordered result by using below method with nested key path to be reordered as the 2nd paramater

      const reordered = reOrder(schoolData, 'classes.studentData.students.studentName')

      So studentName reordered object will look like below

      {
          "schoolName":"Doe School",
          "address":"Stockholm",
          "classes":[
              {
                  "className":"Class 01",
                  "year":"Year 01",
                  "studentData":{
                      "students":[
                      {
                          "studentName":"Alex Numan",
                          "age":15
                      },
                      {
                          "studentName":"Joe Max",
                          "age":20
                      },
                      {
                          "studentName":"John Doe",
                          "age":18
                      },
                      {
                          "studentName":"Philip Larry",
                          "age":16
                      },
                      {
                          "studentName":"Remy Max",
                          "age":21
                      }
                      ]
                  }
              },
              {
                  "className":"Class 02",
                  "year":"Year 01",
                  "studentData":{
                      "students":[
                      {
                          "studentName":"Andersson",
                          "age":15
                      },
                      {
                          "studentName":"Bale Larry",
                          "age":16
                      },
                      {
                          "studentName":"Joe Max",
                          "age":20
                      },
                      {
                          "studentName":"John Doe",
                          "age":18
                      }
                      ]
                  }
              }
          ]
          }