0

I have this kind of array with objects. In each object except properties i have another array with objects called "secondary_fields". I need to access the last element in the secondary_fields array (secondary_fields[3]) where i have value of when it is created and after that to sort all of the objects

by the date time based on the value inside for example "2020-01-13 17:42:51";

But not with ES6 because i am using old platform where the ES6 version of Java Script is not supported

let arr = 
  [
    {
        "external": false,
        "link": "--",
        "direct": false,
        "display_field": "new best article",
        "id": "kb_knowledge:33",
        "secondary_fields": [
          {
            "display_value": "Test Admin",
            "name": "author",
            "label": "Author",
            "type": "reference",
            "value": "xx"
          },
          {
            "display_value": "25",
            "name": "sys_view_count",
            "label": "View count",
            "type": "integer",
            "value": "25"
          },
          {
            "display_value": "2020-01-13 09:44:54",
            "name": "sys_updated_on",
            "label": "Updated",
            "type": "glide_date_time",
            "value": "2020-01-13 17:44:54"
          },
          {
            "display_value": "2020-01-13 09:42:51",
            "name": "sys_created_on",
            "label": "Created",
            "type": "glide_date_time",
            "value": "2020-01-13 17:42:51"
          }
        ],
      },


      {
        "external": false,
        "link": "--",
        "direct": false,
        "display_field": "How to connect the iPod to Wi-Fi",
        "id": "kb_knowledge:11",
        "secondary_fields": [
          {
            "display_value": "John",
            "name": "author",
            "label": "Author",
            "type": "reference",
            "value": "xxnnx"
          },
          {
            "display_value": "16",
            "name": "sys_view_count",
            "label": "View count",
            "type": "integer",
            "value": "16"
          },
          {
            "display_value": "2019-12-18 08:18:08",
            "name": "sys_updated_on",
            "label": "Updated",
            "type": "glide_date_time",
            "value": "2019-12-18 16:18:08"
          },
          {
            "display_value": "2019-10-21 12:27:22",
            "name": "sys_created_on",
            "label": "Created",
            "type": "glide_date_time",
            "value": "2019-10-21 19:27:22"
          }
        ],
      }
  ]

georgeawg
  • 48,608
  • 13
  • 72
  • 95

2 Answers2

1

You can make use of the array method's sort method and sort on display_value.

arr.sort(function(a, b) {
    return new Date(a.secondary_fields[3].display_value) - new Date(b.secondary_fields[3].display_value)
})

Edit

Or I guess in your case,

return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
Mike K
  • 7,621
  • 14
  • 60
  • 120
0

let arr = 
  [
    {
        "external": false,
        "link": "--",
        "direct": false,
        "display_field": "new best article",
        "id": "kb_knowledge:33",
        "secondary_fields": [
          {
            "display_value": "Test Admin",
            "name": "author",
            "label": "Author",
            "type": "reference",
            "value": "xx"
          },
          {
            "display_value": "25",
            "name": "sys_view_count",
            "label": "View count",
            "type": "integer",
            "value": "25"
          },
          {
            "display_value": "2020-01-13 09:44:54",
            "name": "sys_updated_on",
            "label": "Updated",
            "type": "glide_date_time",
            "value": "2020-01-13 17:44:54"
          },
          {
            "display_value": "2020-01-13 09:42:51",
            "name": "sys_created_on",
            "label": "Created",
            "type": "glide_date_time",
            "value": "2020-01-13 17:42:51"
          }
        ],
      },


      {
        "external": false,
        "link": "--",
        "direct": false,
        "display_field": "How to connect the iPod to Wi-Fi",
        "id": "kb_knowledge:11",
        "secondary_fields": [
          {
            "display_value": "John",
            "name": "author",
            "label": "Author",
            "type": "reference",
            "value": "xxnnx"
          },
          {
            "display_value": "16",
            "name": "sys_view_count",
            "label": "View count",
            "type": "integer",
            "value": "16"
          },
          {
            "display_value": "2019-12-18 08:18:08",
            "name": "sys_updated_on",
            "label": "Updated",
            "type": "glide_date_time",
            "value": "2019-12-18 16:18:08"
          },
          {
            "display_value": "2019-10-21 12:27:22",
            "name": "sys_created_on",
            "label": "Created",
            "type": "glide_date_time",
            "value": "2019-10-21 19:27:22"
          }
        ],
      }
  ]
  
arr.sort(function(a, b){
    return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
}) 
console.log(arr)
Harion
  • 225
  • 1
  • 8