I want to use if statement in a function such that **"if there is x ,don't continue function" ** "else ,continue the function".... my code is like below,i dont know why it continue doing the function!(it create calendar) plz help me
const calendars = await Calendar.getCalendarsAsync(
Calendar.EntityTypes.EVENT
);
const filterdcalender = calendars.filter((e) => e.name === "new Calendar");
if (filterdcalender ==! undefined || filterdcalender ==! null) {
console.log('✅ Calender already exists');
console.log({ filterdcalender });
return;
} else{const defaultCalendarSource =
Platform.OS === 'ios'
? await getDefaultCalendarSource()
: { isLocalAccount: true, name: 'new Calendar' };
// ///////////////////////////////////////////////////new calendar
const newCalendarID = await Calendar.createCalendarAsync({
title: 'Title New Calendar',
color: 'blue',
entityType: Calendar.EntityTypes.EVENT,
sourceId: defaultCalendarSource.id,
source: defaultCalendarSource,
name: 'name New Calendar',
ownerAccount: 'personal',
accessLevel: Calendar.CalendarAccessLevel.OWNER,
});
console.log(`Your new calendar ID is: ${newCalendarID}`);
}}
First of all
a ==! bisn't doing what you probably think it does. Because semantically it's equivalent toa == (!b). Use!==instead.Second
Array.filter()will never returnnullorundefined. It will always return an array (which may be empty if no value meeting the condition is found). Thusfilteredcalender !== null || fillteredcalendar !== undefinedwill always betrueYou could either check the length of the filtered array
Or you could make use of
Array.find()