0

Bit of a lengthy one so those of you who like a challenge (or I'm simply not knowledgeable enough - hopefully it's an easy solution!) read on!

(skip to the actual question part to skip the explanation and what I've tried)

Problem

I have a site that has a dataset that contains an object with multiple objects inside. Each of those objects contains an array, and within that array there are multiple objects. (yes this is painful but its from an API and I need to use this dataset without changing or modifying it.) I am trying to filter the dataset based of the key-value pairs in the final object. However, I have multiple filters being executed at once.

Example of Path before looping which retrieves the key-value pair needed for one hall.

["Hamilton Hall"]["Hire Options"][2].Commercial

After Looping Path of required key-value pair for all halls, not just one (the hall identifier is stored):

[0]["Hire Options"][2].Commercial

Looping allows me to check each hall for a specific key-value pair (kind of like map or forEach, but for an object).

After getting that out of the way back to the question.

How would I go about filtering which of the looped objects are displayed?

What I have Tried

(userInput is defined elsewhere - this happens on a btn click btw)

    let results = Object.keys(halls);
    for (key of results) {
        let weekend = [halls[ `${key}` ][ 'Hire Options' ][4][ 'Weekend function' ]];
        if(userInput == weekend) {
            outputAll([halls[ `${key}` ]]);
        }
    }

That filters it fine. However, I run into an issue here. I want to filter by multiple queries, and naturally adding an AND into the if statement doesn't work. I also dont want to have 10 if statements (I have 10+ filters of various data types I need to sort by).

I have recently heard of ternary operators, but do not know enough about them to know if that is the correct thing to do? If so, how? Also had a brief loook at switches, but doesnt seem to look like what I want (correct me if I am wrong.)

Actual Question minus the babble/explanation

Is there a way for me to dynamically modify an if statements conditions? Such as adding or removing conditions of an if statement? Such as if the filter for 'a' is set to off, remove the AND condition for 'a' in the if statement? This would mean that the results would only filter with the active filters.

Any help, comments or 'why haven't you tried this' remark are greatly appreciated!

Thanks!

Just for extra reference, here is the code for retrieving each of the objects from the first object as it loops through them:

(Looping Code)

        halls = data[ 'Halls' ];
        let results = Object.keys(halls);
        for (key of results) {
            let arr = [halls[ `${key}` ]];
            outputAll(arr);
        }
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Heretic Monkey Jul 15 '21 at 13:23

1 Answers1

1

You can use Array.filter on the keys array - you can structure the logic for a match how you like - just make it return true if a match is found and the element needs to be displayed.

let results = Object.keys(halls);
results.filter(key => {
    if (userInput == halls[key]['Hire Options'][4]['Weekend function']) {
      return true;
    }
    if (some other condition you want to match) {
      return true;
    }
    return false;
}).forEach(key => outputAll([halls[key]]));
James
  • 20,957
  • 5
  • 26
  • 41
  • Awesome, that works! Thanks for keeping the same variables etc. too – Elliot Cullen Jul 15 '21 at 13:46
  • I have run into another issue. How I am filtering is a toggle so that if the toggle is checked then filter. This works. However, when it is unchecked it still filters. When the toggle is unchecked I want it to simply remove the filter, e.g if parking filter unchecked, don't check for parking. (Also tried to add more filters - copy/paste - and that didn't work) – Elliot Cullen Jul 16 '21 at 00:10