首页 > 解决方案 > 使用嵌套对象和数组获取大对象的值

问题描述

我正在尝试编写一个自定义过滤器,您可以在其中键入“字符串”,它将过滤大对象中的所有值并查看它是否存在。

示例对象:

{
    "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
  }

查看此对象是否具有值的“字符串”的最佳方法是什么。我必须处理大量这些对象,并希望让人们使用全局搜索。因此,如果我键入“CLS”,它将返回所有带有 integrationClassId 且其中包含“CLS”的对象。然后,如果他们键入“MUSIC 1ST GRD”,它将返回具有该名称的 schClass.name 的所有对象。

我的思考过程是将整个事情扁平化为一个简单的值数组。我将如何使用所有这些值制作 1 个数组?我在这里混合了对象、数组和对象的数组!

标签: javascriptarraysfilter

解决方案


这可能不是最好的解决方案,但它确实有效。我创建了一个函数,它可以使用任何带有嵌套对象/数组的对象或数组,然后返回给您一个值数组。然后,这允许您在数组上使用 indexOf 来查看它是否存在。或者您可以简单地使用 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;
        }

推荐阅读