I have two arrays I want to compare. The state of each object could be either true, false or null depending on what the user has selected.
I then want to compare the two arrays, and push the name of the activity into a separate array if they match each other.
If it compares the first one for example, which would be "free", and the user has checked this, making { free: true }, then this activity ("Go to the cinema") will not be pushed into the new array.
//user input
filtersChecked: [
{ free: null },
{ indoors: null },
{ city: null },
{ winter: null },
{ physical: null },
],
//compared against:
name: "Go to the cinema",
filters: [
{ free: false },
{ indoors: true },
{ city: true },
{ winter: null },
{ physical: false },
],
What I'm struggling with is comparing the "index" of the objects state if that makes any sense...
My attempt looks like this:
function checkFilter(index, isChecked) {
if (isChecked) {
model.input.filtersChecked[index] = true;
} else if (!isChecked) {
model.input.filtersChecked[index] = false;
}
}
function suggestActivities() {
let userFilters = model.input.filtersChecked;
let activityFilters;
let tempArray = [];
for (let i = 0; i < model.data.activities.length; i++) {
activityFilters = model.data.activities[i].filters;
for (let j = 0; j < userFilters.length; j++) {
if (userFilters[j] === activityFilters[j]) {
tempArray.push(model.data.activities[i].name);
console.log(tempArray);
continue;
}
}
}
console.log(tempArray);
}
I hope it's detailed enough. Been stuck on this for way too long now.
Also worth a mention that I would like it to be vanilla JavaScript, since this is a school assignment. So no jQuery please!
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire