React Native / Javascript过滤对象的嵌套数组

时间:2020-07-22 10:08:29

标签: javascript arrays react-native filter

我有一个Json文件,其中包含这样的对象:

  {
    "id": 2407,
    "amount": 120000,
    "rights": {
      "edit": true,
      "level": 0,
      "delete": false,
      "addSubRow": true,
      "duplicate": true
    },
    "comment": "plateforme ESB ajout le 1/02/2020\r\n1er exercice 150k€ pour 1 an\r\ndébut abonnement janv 2020 => 9 mois\r\nsoit 12 500 * 9 = 112 500 €",
    "subRows": [
      {
        "id": 2297,
        "amount": 112500,
        "rights": {
          "edit": true,
          "level": 1,
          "delete": true,
          "addSubRow": true,
          "duplicate": true
        },
        "comment": "1er exercice 150k€ pour 1 an\r\ndébut abonnement janv 2020 => 9 mois\r\nsoit 12 500 * 9 = 112 500 €",
        "version": "Exploitation - En cours",
        "quantity": 9,
        "pv_id_unit": {
          "key": "MOIIS",
          "label": "Mois"
        },
        "fiscal_year": "2019-2020",
        "provider_id": {
          "key": 2221,
          "label": "SOFTWARE AG"
        },
        "pv_id_phase": {
          "key": null,
          "label": null
        },
        "external_ref": null,
        "internal_ref": null,
        "pv_id_status": {
          "key": "ACTIV",
          "label": "Actif"
        },
        "pv_id_company": {
          "key": "GIFFI",
          "label": "GIFI"
        },
        "pv_id_program": {
          "key": null,
          "label": null
        },
        "pv_id_service": {
          "key": "PLAFORDEC20",
          "label": "Plate Forme D'Échange"
        },
        "pv_id_subject": {
          "key": null,
          "label": null
        },
        "operation_date": "27/12/2019",
        "price_per_unit": null,
        "pv_id_activity": {
          "key": "NULL",
          "label": ""
        },
        "pv_id_business": {
          "key": "GROPE",
          "label": "Groupe"
        },
        "pv_id_currency": {
          "key": null,
          "label": null
        },
        "pv_id_resource": {
          "key": "ABOLICES",
          "label": "Abonnement Licences"
        },
        "pv_id_component": {
          "key": "MILTRAE",
          "label": "Millenium Transverse"
        },
        "pv_id_line_type": {
          "key": "COMMITMENT",
          "label": "Engage"
        },
        "tracking_number": "contrat 68971/0007",
        "pv_id_account_nb": {
          "key": "651101",
          "label": "651101"
        },
        "pv_id_department": {
          "key": "DSISI",
          "label": "Dsi"
        },
        "pv_id_workstream": {
          "key": "MILUM",
          "label": "Millenium"
        },
        "pv_id_std_service": {
          "key": null,
          "label": null
        },
        "out_initial_budget": false,
        "pv_id_service_type": {
          "key": null,
          "label": null
        },
        "pv_id_std_resource": {
          "key": "SOF03",
          "label": "Sof03-Location Et Redevances De Logiciels"
        },
        "pv_id_activity_type": {
          "key": null,
          "label": null
        },
        "pv_id_concerned_period": {
          "key": "03DEC",
          "label": "03 Decembre"
        },
        "pv_id_expenditure_type": {
          "key": "CHAGE",
          "label": "Charge"
        },
        "pv_id_std_resource_type": {
          "key": null,
          "label": null
        }
      }
    ],
    "version": "Exploitation - En cours",
    "quantity": null,
    "pv_id_unit": {
      "key": "HEURE",
      "label": "Heure"
    },
    "fiscal_year": "2019-2020",
    "provider_id": {
      "key": 1924,
      "label": "ABC INFORMATIQUE"
    },
    "pv_id_phase": {
      "key": null,
      "label": null
    },
    "external_ref": null,
    "internal_ref": null,
    "pv_id_status": {
      "key": "ACTIV",
      "label": "Actif"
    },
    "pv_id_company": {
      "key": "GIFFI",
      "label": "GIFI"
    },
    "pv_id_program": {
      "key": null,
      "label": null
    },
    "pv_id_service": {
      "key": "PLAFORDEC20",
      "label": "Plate Forme D'Échange"
    },
    "pv_id_subject": {
      "key": "IBMCCMIL",
      "label": "Ibm C.C. Mil Forfait"
    },
    "operation_date": null,
    "price_per_unit": null,
    "pv_id_activity": {
      "key": "NULL",
      "label": ""
    },
    "pv_id_business": {
      "key": "GROPE",
      "label": "Groupe"
    },
    "pv_id_currency": {
      "key": null,
      "label": null
    },
    "pv_id_resource": {
      "key": "ABOLICES",
      "label": "Abonnement Licences"
    },
    "pv_id_component": {
      "key": "MILTRAE",
      "label": "Millenium Transverse"
    },
    "pv_id_line_type": {
      "key": "BUDGET",
      "label": "Budget"
    },
    "tracking_number": null,
    "pv_id_account_nb": {
      "key": "651101",
      "label": "651101"
    },
    "pv_id_department": {
      "key": "DSISI",
      "label": "Dsi"
    },
    "pv_id_workstream": {
      "key": "MILUM",
      "label": "Millenium"
    },
    "pv_id_std_service": {
      "key": null,
      "label": null
    },
    "out_initial_budget": false,
    "pv_id_service_type": {
      "key": null,
      "label": null
    },
    "pv_id_std_resource": {
      "key": "SOF03",
      "label": "Sof03-Location Et Redevances De Logiciels"
    },
    "pv_id_activity_type": {
      "key": null,
      "label": null
    },
    "pv_id_concerned_period": {
      "key": "01OCT",
      "label": "01 Octobre"
    },
    "pv_id_expenditure_type": {
      "key": "CHAGE",
      "label": "Charge"
    },
    "pv_id_std_resource_type": {
      "key": null,
      "label": null
    }
  },

我有一个数据数组,其中包含数百个这些对象,并且该数组用于呈现平面列表。

我正在尝试实现一个搜索栏,当用户键入和更改单位列表时,该搜索栏将过滤所有数据。

所以我在searchBar的onChangeText道具上调用了此函数:

    _handleSearch = (text) => {
    const formatText = text
    const data = _.filter(this.state.fullData, item => {
      for (object in item) {
        let filt = item[object] && item[object]['label'] ? item[object]['label'] : item[object]
        if (filt !== null  && filt.toString() != '[object Object]' && filt !== true && filt !== false) {
          if (item.subRows) {
            const dataE = _.filter(item.subRows, item => {
              for (object in item) {
                let filtE = item[object] && item[object]['label'] ? item[object]['label'] : item[object]
                if (filtE !== null  && filtE.toString() != '[object Object]' && filtE !== true && filtE !== false) {
                  if (filtE.toString().includes(formatText)) {
                    return filtE.toString()
                  }
                }
              }
            })
            for (var i in dataE) {
              for (var y in dataE[i]) {
                return dataE[i][y] && dataE[i][y]['label'] ? dataE[i][y]['label'] : dataE[i][y]
              }
            }
          }
          if (filt.toString().includes(formatText)) {
            return filt.toString()
          }
        }
      }
    })
    this.setState ({ searchedText: text, data })
  }

每次用户键入时,它都会过滤数据数组并删除所有不包含searchedText的对象。然后,它会更新状态以重新呈现单位列表。

制作 if(item.subRows)部分,以便如果用户键入仅包含在子行中的内容,它将返回至少一个子行包含searchedText(因为行可以具有多个子行,这就是为什么subRows是数组的原因。

所有这些工作正常,但是我想要的是还过滤subRows数组以仅呈现正确的子行,而不呈现所有子行。

我在dataE上创建的过滤器有效,因此,如果我 console.log(dataE),它仅包含正确的子行。但是它不会更新完整数据,因此在我的列表中,我仍然拥有项目的所有子行。

感谢所有愿意花时间帮助我的人。此外,即使您没有解决方案,但您认为我可以用一种更简单的方式来完成所有这些工作,我也会接受任何建议。我仍然是React Native / Javascript的入门者,因此我可能做错了方法。

0 个答案:

没有答案