I have output two different arrays and it needs to check duplicates. In order to do that I want to get those two arrays in one function if possible. So the question from my side is, how should I fix that.
function loadLocalStorage() {
let storage = JSON.parse(localStorage.getItem('favoList')) || [];
let listOfFavorites = document.getElementById("favorites");
let emptyArray = '';
if(storage.length > 0) {
for(var i = 0; i < storage.length; i++) {
let idNumberJoke = storage[i].id;
emptyArray +=
`<li><input type="checkbox" id='${idNumberJoke}'/> User title: ${storage[i].joke}</li>`;
listOfFavorites.innerHTML = emptyArray;
checkDuplicate(idNumberJoke); //this will output for instance 590, 95, 550, 126, 72, 124
}
} else {
return false;
}
}
function checkDuplicatesArray(jokeId, storage) {
let jokeIdNumber = jokeId;
let storeJoke = storage;
storeJoke.push(jokeIdNumber);
console.log(storeJoke); // will output [322, 475, 517, 418, 404, 90, 514, 205, 398, 182]
}
You need to clarify what you mean (or probably your own thinking on this subject). What do you mean ‘the two arrays are separated’? Functions don’t “have” arrays, as such - do you mean that they are local to the functions? The second function you wrote in your first post doesn’t return anything and the first one seems to (sometimes) return ‘false’.
In general, I’d say that localStorage and JSON.parse are too complicated to be using until you understand functions properly. Try writing something simpler, perhaps, to get the hang of how to call and return from functions.
Want to iterate through both arrays and one of this two is looping through the localstorage. So at the end it should be blocked from adding when there is a joke stored already on localstorage.