jeudi 30 mars 2017

Remove value from array if it isn't present in another array Javascript

I have an Ember application with which I am building a notification system, if the notification gets dismissed by my user I store the notifications UUID in my cookie so it doesn't get displayed again.

After a month my notifications get deleted from the database, what I am trying to do is check my database and get an array of all the notifications and then compare the UUIDS to my cookies array.

If a UUID exists in my cookies array that doesn't exist in my database I want to delete it from the cookies array.

Here is my code.

this.get('userNotificationServices').fetch().then(allNotifications => {
    if (this.getCookie("dismissed-notifications")) {
        var notificationIdsInCookie = this.getCookie("dismissed-notifications").split(',');
        notificationIdsInCookie.forEach(function (value) {
            var index = allNotifications.findIndex(x => x.id === value);
            if (index === -1) {
                notificationIdsInCookie.splice(index, 1);
            }
        });

        this.setCookie("dismissed-notifications", notificationIdsInCookie);
    }
});

At the moment it seems to be removing the wrong values in the array, for example it removes the last array value instead of the first and then removes every value from the array. Am I doing something wrong with my filtering / is my logic wrong?

Thank you.




Aucun commentaire:

Enregistrer un commentaire