Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to remove same elements from array and add count to other objects javascript?

I have 1 object (it came from localstroage). But there are some dublicates. how can i remove it from easy way?

"mainchart" is my array object.

function check() {
  for (let i1 = 0; i1 < mainchart.length; i1++) {
    for (let i2 = 0; i2 < mainchart.length; i2++) {  
      if (mainchart[i1].id === mainchart[i2].id) {
        console.log(mainchart[i1] , mainchart[i2]);
      }
    }
  }
}

check();

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

An easy way to remove duplicate values from an array is converting the array in to a set (collection of unique values), putting the values of the set in a new array:

let array1 = ["a", "b", true, 3, 67, true, "a"];

let array2 = [...new Set(array1)];

console.log(array2); // [ 'a', 'b', true, 3, 67 ]

Hope it can help you 🦄

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading