Finding value inside an object, that's within an array

Stian Sundby

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!

Arky Asmal

Here is a sample of how you could do this comparison, with the subproblems you could break it down into. Use it as a template with how to solve your assignment's problem. Let me know if it helps.

const userInput = [
    { free: null },
    { indoors: null },
    { city: null },
    { winter: null },
    { physical: null },
 ]

const compared = {
    name: "Go to movies"
    filters: [
       { free: false },
       { indoors: true },
       { city: true },
       { winter: null },
       { physical: false },
    ]
}
const tempArr = []

function compareObjects (userInput, compared) {
    //loop through userInput filters
    for (let i=0; i<userInput.length; i++){
       //grab key from current index of array (like "free", "indoors", etc)
       let key = Object.keys(userInput[i])[0]

        //compare stored filters with user filters
        //if not equal, we can stop searching through the filters entirely and exit the function,
        // since if one of them doesnt match, we wont push at all
        //Note, I can reuse the userInput key for the compared keys, because both arrays are of equal length. 
        //If they weren't the same length, the keys wouldn't match, and you would have to sort the array first
        //so the index order and keys matched for both inputs.   
        if(userInput[i][key] !== compared.filters[i][key]) return
    }
    // if we made it through this loop without exiting the function, we can 
    // push the name since it means they all matched
    tempArr.push(compared.name)
}

//call the function and pass in variables
compareObjects(userInput, compared)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Render a property of an object, within array, inside React Component's state

Finding the index of an object within an array efficiently

Finding a property within a JavaScript array of object

ng-class - finding a value inside object

finding element inside array of object mongodb

get value of an array's items within an object using the key

Trying to get a length on a key value on an object which is inside an object within an array of objects

get value within object in an array

PHP PDO: Search array value inside of MySQL's JSON object

Changing the values inside an object within an array

finding undefined or null value from object inside object

Using jq, get value of a specific key which is inside a shell-defined JSON object within an array

Finding patterns within array

filter multiple array of object in which based on contractNumber finding average it's value in javascript

Finding the highest value of object's field with specified _id in array of objects in mongodb

Finding a value within a range(condition) in each array of values in google sheets

Remove object based on an array value inside the object

Finding a value inside a list

jQuery - how to find a specific JavaScript object inside an array within an object?

Finding duplicate and unique value in array and converting a string to array in object

finding index of object in array in js accordign to value in array

Updating value of an object within an array life cycle

JavaScript - Replacing array value within object

Group duplicate of same value within object of an array

Cant access object value from within an array

Filtering array value of object within objects

Javascript: Finding an object's index in an array, not knowing the index, only the object

How to form a exact query for finding id's inside array

What's the correct syntax for finding an element inside of an array with mongoose?