-1

I'm trying to write a custom filter where you can type a "string" and it will filter all the values in a large object and see if it exists.

Example Object:

{
    "status": "active",
    "terms": [
      {
        "sourcedId": "AS8032021110",
        "type": "academicSession",
        "status": "",
        "startDate": null,
        "endDate": null,
        "responseMsg": "",
        "responseInt": 0
      },
      {
        "sourcedId": "AS8032021120",
        "type": "academicSession",
        "status": "",
        "startDate": null,
        "endDate": null,
        "responseMsg": "",
        "responseInt": 0
      }
    ],
    "integrationClassId": "CLS170316",
    "integrationSiteId": "ENT803",
    "grades": [
      "01"
    ],
    "lastSync": "11\/13\/2020 5:59:59 PM",
    "koSync": null,
    "dropDate": null,
    "id": 0,
    "userLinkId": 0,
    "classId": 0,
    "schClass": {
      "classId": 0,
      "siteId": 0,
      "userLinkCount": 0,
      "integrationType": 10,
      "userLinkId": 0,
      "name": "MUSIC 1ST GRD-1MUSIC\/3",
      "nickName": null,
      "appId": null,
      "deleted": null,
      "integrationId": "",
      "responseMsg": "",
      "responseInt": 0
    },
    "deleteDate": null,
    "responseMsg": "",
    "responseInt": 0
  }

What would be the best approach to see if this object has a "string" for a value. I have to work with a large array of these objects, and want to let people use a global search. So if I typed "CLS" it would return all objects with integrationClassId that has "CLS" in it. Then if they typed "MUSIC 1ST GRD" it would return all the objects with schClass.name with that name.

My thought process was to flatten this entire thing into a 1 simple array of values. How would I make 1 array with just all of these values? I have a mix of arrays of objects, array, and object in this!

Speedy059
  • 629
  • 10
  • 24

1 Answers1

0

This may not be the best solution but it works. I created a function that can take any Object or Array with nested Objects/Arrays and then returns to you a single Array of values. This then allows you to use indexOf on the array to see if it exists. Or you can simply String([thereturnarray]).toLowerCase().indexOf(mySearchTxt.toLowerCase()).

/**
         * You can pass in any size of an object/array and it will return a flatten array of values. It's deep search.
         * @param obj
         * @returns {[]}
         */
        function returnValues(obj) {
            if (obj === null || obj === undefined) {
                return;
            }
            let values = Object.values(obj);
            let flatValues = [];
            for (let i = 0; i < values.length; i++) {
                if (values[i] !== null && values[i] !== undefined) {

                    //If the value is an Object, and not an array, we will process it here
                    if (typeof values[i] === 'object' && !Array.isArray(values[i])) {
                        returnValues(values[i]).forEach((val) => {
                            flatValues.push(val);
                        });
                    }

                    //If the value is an array, we will process it here. We have to iterate over arrays.
                    if (Array.isArray(values[i])) {
                        values[i].forEach((obj) => {
                            returnValues(values[i]).forEach((val) => {
                                flatValues.push(val);
                            });
                        });

                    }

                    //If it's just a value that isn't a Array or Object, just process it immediately
                    if (typeof values[i] !== 'object' && !Array.isArray(values[i])) {
                        //Check if it's a string, if so, check if it's empty and continue.
                        if (typeof values[i] === 'string' && values[i].length === 0){
                            continue;
                        }
                        flatValues.push(values[i]);
                    }
                }
            }
            return flatValues;
        }
Speedy059
  • 629
  • 10
  • 24