Javascript Project to Remove Duplicates From Array of Objects in Browser

You are currently viewing Javascript Project to Remove Duplicates From Array of Objects in Browser

Javascript Project to Remove Duplicates From Array of Objects in Browser

obj.<span class="hljs-property">arr</span> = obj.<span class="hljs-property">arr</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">value, index, self</span>) =></span>
  index === self.<span class="hljs-title function_">findIndex</span>(<span class="hljs-function">(<span class="hljs-params">t</span>) =></span> (
    t.<span class="hljs-property">place</span> === value.<span class="hljs-property">place</span> && t.<span class="hljs-property">name</span> === value.<span class="hljs-property">name</span>
  ))
)
 

<span class="hljs-keyword">const</span> uniqueArray = obj.<span class="hljs-property">arr</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">value, index</span>) =></span> {
  <span class="hljs-keyword">const</span> _value = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>(value);
  <span class="hljs-keyword">return</span> index === obj.<span class="hljs-property">arr</span>.<span class="hljs-title function_">findIndex</span>(<span class="hljs-function"><span class="hljs-params">obj</span> =></span> {
    <span class="hljs-keyword">return</span> <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>(obj) === _value;
  });
});
 

<span class="hljs-keyword">const</span> <span class="hljs-title function_">isPropValuesEqual</span> = (<span class="hljs-params">subject, target, propNames</span>) =>
  propNames.<span class="hljs-title function_">every</span>(<span class="hljs-function"><span class="hljs-params">propName</span> =></span> subject[propName] === target[propName]);
 
<span class="hljs-keyword">const</span> <span class="hljs-title function_">getUniqueItemsByProperties</span> = (<span class="hljs-params">items, propNames</span>) =>
  items.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">item, index, array</span>) =></span>
    index === array.<span class="hljs-title function_">findIndex</span>(<span class="hljs-function"><span class="hljs-params">foundItem</span> =></span> <span class="hljs-title function_">isPropValuesEqual</span>(foundItem, item, propNames))
  );
 

<span class="hljs-keyword">const</span> <span class="hljs-title function_">getUniqueItemsByProperties</span> = (<span class="hljs-params">items, propNames</span>) => {
  <span class="hljs-keyword">const</span> propNamesArray = <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>(propNames);
 
  <span class="hljs-keyword">return</span> items.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">item, index, array</span>) =></span>
    index === array.<span class="hljs-title function_">findIndex</span>(<span class="hljs-function"><span class="hljs-params">foundItem</span> =></span> <span class="hljs-title function_">isPropValuesEqual</span>(foundItem, item, propNamesArray))
  );
};
 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply