JavaScript : How to find max number of Array Object.

You can use the reduce() method to loop through the array of objects and keep track of the maximum id value found so far. Here is an example code snippet:

const data = [
  {
    "id": 1,
    "docNo": "string",

  },
  {
    "id": 2,
    "docNo": "string",
  },
  {
    "id": 3,
    "docNo": "string",

  }
];

const maxId = data.reduce((max, obj) => {
  return obj.id > max ? obj.id : max;
}, 0);

console.log(maxId); // Output: 3

In this example, the reduce() method initializes the max variable to 0 as the initial value. Then, for each object in the array, it checks if the id property is greater than the current max value. If so, it updates the max variable to the new id value. Finally, the reduce() method returns the maximum id value found.

Leave a Reply

Your email address will not be published. Required fields are marked *