【问题标题】:Check if a string contains another string React Native检查一个字符串是否包含另一个字符串 React Native
【发布时间】:2021-07-22 07:29:05
【问题描述】:

我正在尝试像这样过滤一组数据:

let data = 
[
   {
    "approval": "TPED",
    "manufacturer": "Chesterfield"
   },
   {
    "approval": "BV",
    "manufacturer": "Faber"
   }
]

let approvalVariable = "TP"
let filteredData = data.filter(x => x.approval.includes(approvalVariable))

console.log(filteredData)

所以如果approvalVariable 是“TP”,我希望新数组是:

[
   {
    "approval": "TPED",
    "manufacturer": "Chesterfield"
   },
]

当我这样做时,它就可以工作了:

let filteredData = data.filter(x => x.approval == approvalVariable)

但是当我尝试时:

x.approval.includes(approvalVariable)

我收到 x.approval.includes 不是对象的错误

我曾经使用 .includes() 让它工作,但现在出了点问题。

任何帮助将不胜感激。

  componentWillMount() {
    this.fetchData();
  }

 fetchData = async () => {
   var fireBaseResponse = firebase.database().ref();
   fireBaseResponse.once('value').then(snapshot => {
     let data1 = [];
     let approval = this.props.navigation.state.params.approval
     let comments = this.props.navigation.state.params.comments
     let designStandard = this.props.navigation.state.params.designStandard
     let diameter = this.props.navigation.state.params.diameter
     let h2Compatible = this.props.navigation.state.params.h2compatible
     let inletThread = this.props.navigation.state.params.inletThread
     let manufacturer = this.props.navigation.state.params.manufacturer
     let specificationNumber = this.props.navigation.state.params.specificationNumber
     let testPressure = this.props.navigation.state.params.testPressure
     let waterCapacity = this.props.navigation.state.params.waterCapacity
     let workingPressure = this.props.navigation.state.params.workingPressure


    snapshot.forEach(item =>{
        const temp = item.val();
        data1.push(temp);
        return false;
      });
////////Filter Method/////////
      if(approval == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.approval.includes(approval))
      }
      if(waterCapacity == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.waterCapacity == waterCapacity)
      }
      if(designStandard== '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.designStandard == designStandard)
      }
      if(diameter == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.diameter == diameter)
      }
      if(inletThread == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.inletThread == inletThread)
      }
      if(workingPressure == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.workingPressure == workingPressure)
      }
      if(comments == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.comments == comments)
      }

      if(manufacturer == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.manufacturer == manufacturer)
      }
      if(testPressure == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.testPressure == testPressure)
      }

      if(specificationNumber == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.specificationNumber == specificationNumber)
      }
      if(h2Compatible == '') {
        console.log("good")
      }
      else {
        data1 = data1.filter(x => x.h2Compatible == h2Compatible)
      }


/////////////////////Filter Method//////////////////

      this.setState({data: data1});

    });
  }
  render(){
    var {navigate} = this.props.navigation;
    let {params} = this.props.navigation.state;
    return(
    <ViewContainer>
        <ScrollView>
         <FlatList
                data = {this.state.data}
                keyExtractor = {(x, i) => i}
                renderItem ={({item}) =>
                    <Text style = {styles.itemText}>
                        Approval: {item.approval} | Manufacturer: {item.manufacturer} | Specification Number: {item.specificationNumber} |
                        H2 Compatible: {item.h2Compatible} | Diameter: {item.diameter} | Water Capacity: {item.waterCapacity} |
                        Inlet Thread: {item.inletThread}{"\n"}
                    </Text>
                }
            />
        </ScrollView>
            <View style ={styles.footer}>
                <TouchableOpacity style ={styles.footerButton} onPress = { () => navigate("ValveSearchScreen")}>
                    <Text style ={styles.footerButtonText}>SEARCH</Text>
                </TouchableOpacity>
            </View>
    </ViewContainer>

    )
  }
}

【问题讨论】:

  • 代码对我来说很好用。你肯定在其他地方有错误。
  • 几天前它对我来说也很好,但由于某种原因,我收到这个错误,说有一个未经处理的承诺拒绝
  • 是的,这与您使用 Promise 的其他代码有关。
  • 现在我们有两个不同的错误,并且显示的代码都是不可能的...请添加一个 minimal, complete and verifiable example 以显示实际问题。
  • @paulgio:如果可能,建议您在snack.expo.io 设置代码

标签: javascript react-native


【解决方案1】:

问题在于它在数组对象中搜索一个名为 include 的属性。显然它找不到它,所以它给了我该属性不存在的警告。为了解决这个问题,我将行更改为

let filteredData = data.filter(x => String(x.approval).includes(approvalVariable));

我希望这对将来的其他人有所帮助,并且您不要像我那样在没有帮助的情况下花费一周的时间来解决这个问题。

【讨论】:

    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多