{"id":1016,"date":"2012-03-31T20:42:07","date_gmt":"2012-04-01T02:42:07","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1016"},"modified":"2012-03-31T20:44:38","modified_gmt":"2012-04-01T02:44:38","slug":"code-of-the-day-get-unique-objects-from-an-array-of-json-objects","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-get-unique-objects-from-an-array-of-json-objects\/","title":{"rendered":"Code of the day: Get unique objects from an array of json objects."},"content":{"rendered":"<p>Inspired from <a href=\"http:\/\/stackoverflow.com\/questions\/9923890\/removing-duplicate-objects-with-underscore-for-javascript\/9925133#9925133\">Removing duplicate objects with Underscore for Javascript<\/a><\/p>\n<p><b>Source:<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n\/**\r\n@function _.uniqObjects\r\n@require Underscore.js and json.stringify\r\n@purpose return an array of objects without duplicated objects.\r\n*\/\r\n_.uniqObjects = function( arr ){\r\n\treturn _.uniq( _.collect( arr, function( x ){\r\n\t\treturn JSON.stringify( x );\r\n\t}));\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Example:<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nvar foo = [ { \"a\" : \"1\" }, { \"b\" : \"2\" }, { \"a\" : \"1\" } ];\r\n_.uniqObject( foo );  \/\/ returns [ { \"a\" : \"1\" }, { \"b\" : \"2\" } ]\r\n\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inspired from Removing duplicate objects with Underscore for Javascript Source: \/** @function _.uniqObjects @require Underscore.js and json.stringify @purpose return an array of objects without duplicated objects. *\/ _.uniqObjects = function( arr ){ return _.uniq( _.collect( arr, function( x ){ return JSON.stringify( x ); })); }; Example: var foo = [ { &#8220;a&#8221; : &#8220;1&#8221; }, &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-get-unique-objects-from-an-array-of-json-objects\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Get unique objects from an array of json objects.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[164,131,187],"class_list":["post-1016","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-javascript","tag-json","tag-underscore"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1016"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":1020,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1016\/revisions\/1020"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}