{"id":666,"date":"2023-03-01T20:23:34","date_gmt":"2023-03-01T20:23:34","guid":{"rendered":"https:\/\/freelance.in.th\/?p=666"},"modified":"2023-03-09T15:27:29","modified_gmt":"2023-03-09T15:27:29","slug":"javascript-how-to-check-duplicate-values","status":"publish","type":"post","link":"https:\/\/freelance.in.th\/index.php\/2023\/03\/01\/javascript-how-to-check-duplicate-values\/","title":{"rendered":"JavaScript : How to check duplicate values."},"content":{"rendered":"\n<p>To check for <strong>duplicate <\/strong>values in an <strong>array of objects<\/strong>, you can use the <code><strong><em>reduce()<\/em><\/strong><\/code> method to create an object where each key represents a unique value and the corresponding value is the number of times that value appears in the array. Here&#8217;s an example code snippet:<\/p>\n\n\n\n<div class=\"wp-block-columns is-not-stacked-on-mobile is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"wp-block-code\"><code>const data = &#91;\n  {\n    \"id\": 1,\n    \"docNo\": \"doc123\",\n  },\n  {\n    \"id\": 2,\n    \"docNo\": \"doc456\",\n  },\n  {\n    \"id\": 3,\n    \"docNo\": \"doc123\",\n  }\n];\n\nconst docNoCounts = data.reduce((counts, obj) =&gt; {\n  counts&#91;obj.docNo] = (counts&#91;obj.docNo] || 0) + 1;\n  return counts;\n}, {});\n\nconsole.log(docNoCounts); \/\/ Output: { \"doc123\": 2, \"doc456\": 1 }\n<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:20%\">\n<a href=\"https:\/\/clicks.pipaffiliates.com\/c?m=6543&#038;c=711675\" referrerpolicy=\"no-referrer-when-downgrade\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ads.pipaffiliates.com\/i\/6543?c=711675\" width=\"120\" height=\"600\" referrerpolicy=\"no-referrer-when-downgrade\"\/><\/a>\n<\/div>\n<\/div>\n\n\n\n<p>In this example, the<strong><em> <code>reduce()<\/code><\/em><\/strong> method initializes an empty object as the initial value. Then, for each object in the array, it checks if the <code>docNo<\/code> property already exists as a key in the object. If it does, it increments the count value by 1. If not, it adds the <code>docNo<\/code> property as a new key in the object with an initial value of 1. Finally, the <code><strong><em>reduce()<\/em><\/strong><\/code> method returns the object with the count values for each <code>docNo<\/code> value.<\/p>\n\n\n\n<p>You can then check if there are any duplicate values by iterating over the <code>docNoCounts<\/code> object and checking if any of the values are greater than 1. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (const docNo in docNoCounts) {\n  if (docNoCounts&#91;docNo] &gt; 1) {\n    console.log(`Duplicate docNo value found: ${docNo}`);\n  }\n}\n<\/code><\/pre>\n\n\n\n<a href=\"https:\/\/clicks.pipaffiliates.com\/c?m=117283&#038;c=711675\" referrerpolicy=\"no-referrer-when-downgrade\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ads.pipaffiliates.com\/i\/117283?c=711675\" width=\"728\" height=\"90\" referrerpolicy=\"no-referrer-when-downgrade\"\/><\/a>\n","protected":false},"excerpt":{"rendered":"<p>To check for duplicate values in an array of objects, you can use the reduce() method to create an object where each key represents a unique value and the corresponding value is the number of times that value appears in the array. Here&#8217;s an example code snippet: In this example, the reduce() method initializes an&#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-666","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\/666","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=666"}],"version-history":[{"count":3,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":737,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/posts\/666\/revisions\/737"}],"wp:attachment":[{"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/categories?post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freelance.in.th\/index.php\/wp-json\/wp\/v2\/tags?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}