{"id":664,"date":"2023-03-01T20:15:31","date_gmt":"2023-03-01T20:15:31","guid":{"rendered":"https:\/\/freelance.in.th\/?p=664"},"modified":"2023-03-09T15:30:16","modified_gmt":"2023-03-09T15:30:16","slug":"javascript-how-to-find-max-number-of-array-object","status":"publish","type":"post","link":"https:\/\/freelance.in.th\/index.php\/2023\/03\/01\/javascript-how-to-find-max-number-of-array-object\/","title":{"rendered":"JavaScript : How to find max number of Array Object."},"content":{"rendered":"\n<p>You can use the <code><strong><em>reduce()<\/em><\/strong><\/code> method to loop through the <em><strong>array of objects<\/strong><\/em> and keep track of the maximum <code>id<\/code> value found so far. Here is an example code snippet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const data = &#91;\n  {\n    \"id\": 1,\n    \"docNo\": \"string\",\n\n  },\n  {\n    \"id\": 2,\n    \"docNo\": \"string\",\n  },\n  {\n    \"id\": 3,\n    \"docNo\": \"string\",\n\n  }\n];\n\nconst maxId = data.reduce((max, obj) =&gt; {\n  return obj.id &gt; max ? obj.id : max;\n}, 0);\n\nconsole.log(maxId); \/\/ Output: 3\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code><strong><em>reduce()<\/em><\/strong><\/code> method initializes the <code><strong>max<\/strong><\/code> variable to <code>0<\/code> as the initial value. Then, for each object in the array, it checks if the <code>id<\/code> property is greater than the current <code>max<\/code> value. If so, it updates the <code>max<\/code> variable to the new <code>id<\/code> value. Finally, the <code><strong><em>reduce()<\/em><\/strong><\/code> method returns the maximum <code>id<\/code> value found.<\/p>\n\n\n\n<a href=\"https:\/\/clicks.pipaffiliates.com\/c?m=103111&#038;c=711675\" referrerpolicy=\"no-referrer-when-downgrade\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ads.pipaffiliates.com\/i\/103111?c=711675\" width=\"728\" height=\"90\" referrerpolicy=\"no-referrer-when-downgrade\"\/><\/a>\n","protected":false},"excerpt":{"rendered":"<p>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: 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[43,44],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript","tag-programming"],"_links":{"self":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/comments?post=664"}],"version-history":[{"count":2,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":738,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/664\/revisions\/738"}],"wp:attachment":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}