How get arrays from two different functions in one function?

How get arrays from two different functions in one function?
0

#1

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]
	}

#2

I’m not sure I understand the question. You can pass both arrays as parameters.

checkDups(array1, array2);

Is this what you’re asking?


#3

But how do I pass that as parameters into one function?


#4

When you’re defining a function, you give it the names of the parameters you want to pass it:

function checkDuplicatesArray(firstArray, secondArray) { // two parameters, firstArray and secondArray

}

When you call the function, you pass it the data you want the function to use:

checkDuplicates(array1, array2);

You can pass as many variables as you want. Just make sure you define them in the function.


#5

I understand, but the two arrays are seperated now. Each function has one array at this moment…so how could I do that.


#6

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.


#7

You’ll have to be clearer on what you are attempting to do with the arrays. What is your end goal?


#8

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.