I save in localstorage an array:
var myLikes = []; myLikes[0] = {'slide':slide_id} localStorage.setItem("myCollection", JSON.stringify(myLikes));
Next, I need to check whether the new value is unique and if not, add it to the myCollection array. I'm trying to do it like this:
var slide_id = $$('#likes').attr('slide_id'); var arrayLikes = JSON.parse(localStorage.getItem('myCollection')); for (var i = 0; i < arrayLikes.length; i++) { if (slide_id == arrayLikes[i].slide) { // dublicate console.log('Dublicate'); } else { console.log('New' + arrayLikes[i].slide); arrayLikes.push({'slide':slide_id}); localStorage.setItem("myCollection", JSON.stringify(arrayLikes)); } }
As a result, the check does not work and all values are added to localstorage, including duplicates + array begins to increase exponentially.
What am I doing wrong and how can I correctly perform the check?