Как проверить, существует ли выбранный объект в localStorage? - PullRequest
0 голосов
/ 24 апреля 2020

Как проверить, существует ли выбранный объект в localStorage? Ниже в моем коде я получаю данные с помощью .getItem (), а затем сохраняю данные с помощью JSON .parse (). Но как я могу проверить, существует ли выбранный объект в retrieveDataArray?

function focusOnClickSaveBtn(event){
   if(event.stopPropagation){
       event.stopPropagation();
       event.preventDefault();
   }

   event.cancelBubble = true;
   event.returnValue = false;
   return false;
}

var saveButton = document.querySelectorAll('.save-job');
var arrayLocalStorage = [];

saveButton.forEach(function(btn, indexBtn){
     btn.addEventListener('click', focusOnClickSaveBtn, false);

     jQuery(btn).unbind().click(function(){   
         const topjobObj = {
             id: jQuery(this).attr('topjob-id'),
             ID: jQuery(this).attr('topjob-id'),
             Phone: jQuery(this).attr('topjob-phone'),
             Name: jQuery(this).attr('topjob-title'),
             Region: jQuery(this).attr('topjob-location'),
             Sparte: jQuery(this).attr('topjob-branche')
         };

         if(!jQuery(this).hasClass('in-local-storage')){
             arrayLocalStorage.push(topjobObj);
             //console.log(arrayLocalStorage);

             localStorage.setItem('Jobs', JSON.stringify(arrayLocalStorage));
             jQuery(this).addClass('in-local-storage');
             jQuery(this).find('.span-text').text('Job saved');

         }else{
             jQuery(this).find('.span-text').text('Save Job');
             jQuery(this).removeClass('in-local-storage');

             var retrieveData = localStorage.getItem('Jobs');
             var retrieveDataArray = JSON.parse(retrieveData);
             //console.log(retrieveDataArray);

             if(// the object clicked exists){
                 //console.log(object.id);
                 //delete the data from the array
             }
             // Here restore the localStore
             localStorage.setItem('Jobs', JSON.stringify(retrieveDataArray));
         }
    });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...