简体   繁体   English

自动完成表单错误:未捕获的TypeError:data.match不是函数

[英]Autocomplete form error: Uncaught TypeError: data.match is not a function

I am working with React and trying to figure out how to return data in an Autocomplete form from a JSON 我正在与React一起工作,试图找出如何从JSON以自动完成形式返回数据

this is the data 这是数据

const falsyData = [{
    'Hello': {
      'what I do'       : 'you just say Hello',
      'which language'  : 'english'
    },
    'Travel': {
      'Etymology' : 'The origin of the word "travel" is...',
      'Purpose'   : 'include recreation'
    },
    'Gambling': {
      'Gamble'  : 'Gambling is the wagering of money...',
      'Origins' : 'Many popular games played in...'
    }
  }];

  render () {
    return (
      <Grid>
            <TextField
                onChange={this._onChange.bind(this)}
                onKeyUp={this._changeInput.bind(this)}
                value={this.state.value} />
            {!!this.state.value.length &&
              <Row>
                <Column>  
                 //HERE I PRINT THE RESULTS OF THE SEARCH
                  <p>{this.state.result}</p>
                </Column>                              
              </Row>
            }
      </Grid>
    );
  }

  _matchData = (input) => {
    let reg = new RegExp(input.split('').join('\\w*').replace(/\W/, ""), 'i');
    return falsyData.map(function(data) {
      if (data.match(reg)) {
        return data;
      }
    });
  }

  _changeInput = (val) => {
    let autoCompleteResult = this._matchData(this.state.value);
    if (autoCompleteResult.length) {
      this.setState({result: autoCompleteResult.join(' ')});
    };
  }

  _onChange = (event) => {
    this.setState({value: event.target.value});
  } 

I need to filter the data, lets say that the user types in A , so in <p>{this.state.result}</p> I want to see everything with the letter A . 我需要过滤数据,可以说用户输入A ,所以在<p>{this.state.result}</p>我想看到所有带有字母A的内容

For more specific: if the user types hello , I want to see hello from falsyData with their attributes and properties. 对于更具体的:如果用户键入你好 ,我想看看hellofalsyData与他们的属性和特性。

BTW BTW

everytime I type in the search bar, this is what I get in the browser console 每次我在搜索栏中键入内容,这就是我在浏览器控制台中看到的内容

Uncaught TypeError: data.match is not a function 未捕获的TypeError:data.match不是函数

falsyData is an array of objects. falsyData是对象数组。 In this block of code: 在此代码块中:

return falsyData.map(function(data) {
  if (data.match(reg)) {
    return data;
  }
});

You're mapping over objects, so data doesn't have a match function, because it isn't a string. 您正在对象上进行映射,因此data没有match功能,因为它不是字符串。

Depending on what you want to match on, change this to match string values within your objects. 根据您要匹配的内容,更改它以匹配对象中的字符串值。 For example, if you just wanted to match on the outer object's key: 例如,如果您只想匹配外部对象的键:

return falsyData.map(function(data) {
  for (var key in data) {
    if (key.match(reg)) {
      return key;
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM