2 回答

TA貢獻1780條經驗 獲得超4個贊
Set
將自動使值唯一。你可以有類似的東西:
// 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');
編輯:我們只想為每個季節保留一個值:
// 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');
添加回答
舉報