如何检查状态是否已存在?

时间:2018-11-14 02:16:34

标签: javascript reactjs

无论如何,我是否可以检查此代码状态下的数字是否已经存在(例如,是否存在一个以上特定数字的实例,等等)?我什么都找不到,我尝试了所有我能想到的方法(Indexof,contains等,甚至将值转换为数组也无济于事)。

const button1 = [{number: 'one', value: '1'},{number: 'two', value: '2'},{number: 'three', value: '3'},{number: 'divide', value: '/'}];

const button2 = [{number: 'four', value: '4'},{number: 'five', value: '5'},{number: 'six', value: '6'},{number: 'add', value: '+'}];

const button3 = [{number: 'seven', value: '7'},{number: 'eight', value: '8'},{number: 'nine', value: '9'},{number: 'subtract', value: '-'}];

const button4 = [{number: 'zero', value: '0'},{number: 'decimal', value: '.'},{number: 'equals', value: '='},{number: 'multiply', value: '*'}];


  class Calculator extends Component {
  constructor(props) {
    super(props);
    this.state = { value: "0"};
    this.handleClick = this.handleClick.bind(this);
  }


 handleClick(evt) {
    const id = evt.target.id;
    const result = evt.target.value;

switch(id) {
    case 'clear':
        this.setState({ value: "0"});
        break;
    case 'equals':
      this.setState(prevState => ({
        value: math.eval(this.state.value)
      }));
        break;

    default: this.setState(prevState => ({
        value: `${prevState.value}${result}`.replace(/([/+\-/*=])([/+\-*=])/g, "$2")
          .replace(/^0+(?=[1-9])/, "")
          .replace(/^0+(?=\.)/, "0")
          .replace(/^0+\B/, "")
          .replace(/\.+/g,".")
      })); 
}


}







render() {
    return(
            <div id="container">
                <Display value={this.state.value} />
                <div>
  {button1.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}

                </div>
                <div>
 {button2.map((el, index)  => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
                </div>
                <div>
 {button3.map((el, index)  => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
                </div>
                <div>
{button4.map((el, index) => <Button onClick={this.handleClick} key={el.index} id={el.number} value={el.value} />)}
                </div>
                <div>
                <Button onClick={this.handleClick} id="clear" value={'clear'}  />
                </div>
            </div>
)

}

}

2 个答案:

答案 0 :(得分:1)

在“单击处理”中,尝试将默认值替换为以下

  handleClick(evt) {
    const id = evt.target.id;
    let result = evt.target.value;

    switch (id) {
      case "clear":
        this.setState({ value: "0" });
        break;
      case "equals":
        this.setState(prevState => ({
          value: math.eval(this.state.value)
        }));
        break;

      default:
        this.setState(prevState => {
          const numbers = prevState.value.split(/[+\-*=]/g);
          const lastNumber = numbers[numbers.length - 1];
          if (result === ".") {
            console.log((lastNumber.match(/\./g) || []).length);
            if (lastNumber.length === 0) {
              result = ""; //Remove if first charracter is decimal
            }
            if ((lastNumber.match(/\./g) || []).length > 0) {
              result = "";
            }
          }
          let value = `${prevState.value}${result}`
            .replace(/([+\-*=])([+\-*=])/g, "$2")
            .replace(/^0+(?=[1-9])/, "")
            .replace(/^0+(?=\.)/, "0")
            .replace(/^0+\B/, "");

          return {
            value
          };
        });
    }
  }

答案 1 :(得分:0)

您要寻找的方法是includes

根据您的其他信息进行编辑:

var positions = [];
for(i = 0; i < this.state.value.length; i++) {
    if(this.state.value.charAt(i) === result) {
        positions.push(i);
    }
}

您必须进行编辑以满足您的特定需求(给您的印象是您正在寻找尚未明确说明的特定内容),但我想这会让您越过障碍。