js对三层数组进行数据筛选

    treeData: [
        {
          MenuName: "信息",
          MenuID: 1,
          menuChildList: [
            {
              MenuName: "个人信息",
              MenuID: "2",
              menuChildList: [
                {
                  MenuName: "审核",
                  MenuID: 3,
                },
                {
                  MenuName: "编辑",
                  MenuID: 4,
                },
              ],
            },
          ],
        },
      ],
      
    /**
     * 对多维数组的筛选
     * @param query          要查询的值
     * @param children       多层级树形的数组的key与查询数据比对 默认 ['label']
     * @param key            多层级树形的数组的key 默认 ['children']
     * @param list           多层级树形的数组
     * @returns {Array}
     */
    filterArrayData(query, key, children, list) {
      var filterObj = function (item) {
        if (item[key].indexOf(query) > -1) return true;
        if (item.hasOwnProperty(children)) {
          item[children] = item[children].filter(function (child) {
            if (child.hasOwnProperty(children)) {
              return filterObj(child);
            } else {
              return child[key].indexOf(query) > -1;
            }
          });
          if (item[children].length > 0) {
            return true;
          }
        } else {
          return child[key].indexOf(query) > -1;
        }
      };
      var filter = list.filter(function (item) {
        return filterObj(item);
      });
      console.log(JSON.stringify(filter));
      this.treeData = filter;
    },
    searchFn() {
      if (this.searchValue) {
        this.filterArrayData(
          this.searchValue,
          "MenuName",
          "menuChildList",
          this.treeData
        );
      } else {
        this.treeData = this.orgtreeData;
      }
    },

你可能感兴趣的:(javascript,开发语言,ecmascript)