title: How to get duplicate elements in an array
body: <p>I am writing code in ReactJs
  I have an Array of object like this</p>
  <pre><code>[
  {
          createdBy: &quot;DEF&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Doe&quot;,
          section: {
              createdBy: &quot;A2C&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
  
  {
          createdBy: &quot;ABC&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Doe&quot;,
          section: {
              createdBy: &quot;ABC&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
  
      {
          createdBy: &quot;ABC&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Doe&quot;,
          section: {
              createdBy: &quot;XYZ&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
  
      {
          createdBy: &quot;A1C&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Wick&quot;,
          section: {
              createdBy: &quot;ABC&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
  ];
  </code></pre>
  <p>here, the only thing same is the &quot;name&quot;, so on the basis of name, I want the duplicate objects to be stored in the new array.</p>
  <p>I have tried it like this</p>
  <pre><code>let temp = [];
  for (const i of response) {
      if (!temp.includes(i)) {
          temp.push(i);
          console.log(&quot;if loop&quot;, i);
      } else {
          console.log(&quot;else loop&quot;, response);
      }
  }
  </code></pre>
  <p>but the control never goes to else as it considers each object as different.</p>
  <p>I need the first occurrence of an object as it is, but when the &quot;name&quot; element gets repeated, that object should get stored in a new array.</p>
  <p>Expected:-</p>
  <pre><code>[
      {
          createdBy: &quot;ABC&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Doe&quot;,
          section: {
              createdBy: &quot;ABC&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
      {
          createdBy: &quot;ABC&quot;,
          createdDate: &quot;2020&quot;,
          lastModifiedDate: &quot;2021&quot;,
          name: &quot;John Doe&quot;,
          section: {
              createdBy: &quot;XYZ&quot;,
              status: &quot;ACTIVE&quot;,
          },
          sectionName: &quot;Mechanical&quot;,
      },
  
  
  ]
  </code></pre>