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

Find all unique values in array and than split it and return something else

My goal is to find all the unique years in this array and than return it as a array. I have almost done it but cant figure out how I will return the split value.

function findUniqueYears() {
  const arr = ['1973-11-01', '2001-01-01', '1999-10-01', '2007-10-01', '2016-06-01', '2005-08-01', '1973-09-01', '1979-12-01', '2001-08-01'];
  for (var i = 0; i < arr.length; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (arr[i].split("-")[0] === arr[j].split("-")[0]) {
        arr.splice(j, 1);
      }
    }
  }
  console.log(arr)
}

>Solution :

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

This should do

const findUniqueYears = (arr) => Array.from(new Set(arr.map(item => item.split('-')[0])))
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