-2

I am working in Aurelia.I want to get highest(max) Id in Integer type from json array .

private list :any;
 this.list = {
      "a_Rows": [
        {
          "id": "1",
          "sname": "amir",
          "sType": "Cheque",
          "semail": "ert",
          },
{
          "id" : "8",
          "sname": "adil",
          "sType": "Cheque1",
          "semail": "abc",
}
]
Aamir Hussain
  • 141
  • 1
  • 10

1 Answers1

2

You can sort list.a_Rows and inside the sort callback use unary operator to convert id to number before comparing. In this example the sort is in descending order

let list = {
  "a_Rows": [{
      "id": "1",
      "sname": "amir",
      "sType": "Cheque",
      "semail": "ert",
    },
    {
      "id": "8",
      "sname": "adil",
      "sType": "Cheque1",
      "semail": "abc",
    }
  ]

}

let highestId = list.a_Rows.sort((a, b) => {
  return +b.id - (+a.id)

})

console.log(highestId[0].id)
brk
  • 48,835
  • 10
  • 56
  • 78