问题过滤器和使用Object.entries

时间:2019-04-18 19:23:35

标签: javascript json filter

我有以下问题。我有一个代码,如果将它单独链接到.filters上也可以工作,也可以单独测试代码,但是当我组合它们时,即使值返回true,也不会返回,所以我认为我没有正确应用returns内的ifs

这不起作用

    const FILTERED_DATA = this.filteredProjects.filter(o => {
      Object.entries(FORM_VALUES).every(([k, v]) => {
        if (isDate(sC) || isDate(eC)) {
          if (this.dateRangeValidator(sC, eC, o.created)) {
            return v === "" || o[k] === v || v === 0 || v === "Invalid Date";
          }
        } else if (isDate(sM) || isDate(eM)) {
          if (this.dateRangeValidator(sM, eM, o.modified)) {
            return v === "" || o[k] === v || v === 0 || v === "Invalid Date";
          }
        } else {
          return true;
        }
      });
    });

作品

    const FILTERED_DATA = this.filteredProjects
      .filter(o => {
        if (isDate(sC) || isDate(eC)) {
          return this.dateRangeValidator(sC, eC, o.created);
        } else if (isDate(sM) || isDate(eM)) {
          return this.dateRangeValidator(sM, eM, o.modified);
        } else {
          return true;
        }
      })
      .filter(o =>
        Object.entries(FORM_VALUES).every(
          ([k, v]) => v === "" || o[k] === v || v === 0 || v === "Invalid Date"
        )
      );

1 个答案:

答案 0 :(得分:2)

您需要return

const FILTERED_DATA = this.filteredProjects
        .filter(o => {
            return Object.entries(FORM_VALUES)
//          ^^^^^^

,并省略最后的返回true