score:1

Accepted answer

Use of Set instead of Array

Great article about Set here

Set will automatically make values uniques. You could have something like :

// Declaration of the Set
const uniqueSet = new Set();

function handleClicked(calendarIconId) {
  // Add the new value inside of the Set
  uniqueSet.add(calendarIconId);
  
  // Turn the set into an array
  const array = Array.from(uniqueSet);

  console.log(array);
}

handleClicked('autumn=2020-08-20');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('spring=2020-04-20');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');


EDIT : We only want to keep one value for each season :

// Declaration of an object. We are going to use the key/value system
const library = {};

function handleClicked(calendarIconId) {
  // We "open" the value to extract the relevant informations
  const [
    key,
    value,
  ] = calendarIconId.split('=');

  // Add the new value inside of the object
  library[key] = value;
  
  // Turn the object into an array, we gotta rebuild the values
  const array = Object.keys(library).map(x => `${x}=${library[x]}`);

  console.log(array);
}

handleClicked('autumn=2020-08-20');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('spring=2020-04-20');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');

score:0

You can do

if(!Array.includes(id)) {
  Array.push(id)
}


Related Query

More Query from same tag