Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:条件检查的快捷语法?_Javascript_Syntax_Shortcut - Fatal编程技术网

Javascript:条件检查的快捷语法?

Javascript:条件检查的快捷语法?,javascript,syntax,shortcut,Javascript,Syntax,Shortcut,假设我有一个函数需要检查与两个不同值之一的匹配。但是,输入非常复杂: function checker(id, value){ if (this.state.users[id].items[value].name === 'onething ' || this.state.users[id].items[value].name === 'theotherthing ' ){ // my action } } 我最后做的是: function checker(id, va

假设我有一个函数需要检查与两个不同值之一的匹配。但是,输入非常复杂:

function checker(id, value){
  if (this.state.users[id].items[value].name === 'onething ' ||
    this.state.users[id].items[value].name === 'theotherthing ' ){
    // my action
  }
}
我最后做的是:

function checker(id, value){
  var name = this.state.users[id].items[value].name
  if (name === 'onething ' || name === 'theotherthing '){
    // my action
  }
}
有没有办法做到这一点:

function checker(id, value){
  if (this.state.users[id].items[value].name === 'onething ' || 'theotherthing '){
    // my action
  }
}

显然,第二种方法比第一种方法需要更少的类型,并且更容易重构。它们如何比较内存/速度?

在ECMAScript 2016中,您可以执行以下操作:

if (['onething ','theotherthing'].includes(this.state.users[id].items[value].name)) {
    //do stuff
}
本声明由以下部分组成:

  • if语句(显然)

  • 数组定义:
    ['onething','theotherthing']

  • 对先前定义的数组调用方法
    includes()

  • 在javascript中,数组是一个对象,其方法与任何其他对象类似。其中一个方法是
    includes()
    ,它检查参数是否包含在数组中。此方法的返回类型是布尔型的,因此它由if语句直接计算,而不进行任何强制转换


    更多关于ECMAScript 2016中的includes()方法的信息,您可以执行以下操作:

    if (['onething ','theotherthing'].includes(this.state.users[id].items[value].name)) {
        //do stuff
    }
    
    本声明由以下部分组成:

  • if语句(显然)

  • 数组定义:
    ['onething','theotherthing']

  • 对先前定义的数组调用方法
    includes()

  • 在javascript中,数组是一个对象,其方法与任何其他对象类似。其中一个方法是
    includes()
    ,它检查参数是否包含在数组中。此方法的返回类型是布尔型的,因此它由if语句直接计算,而不进行任何强制转换

    关于
    includes()
    方法的更多信息

    您可以使用
    -1

    if (['onething ', 'theotherthing '].indexOf(this.state.users[id].items[value].name ) !== -1){
    
    您可以使用
    -1

    if (['onething ', 'theotherthing '].indexOf(this.state.users[id].items[value].name ) !== -1){
    

    您可以使用对象表示法:

    if (this.state.users[id].items in {"onething ":1, "theotherthing ":1}){
    
    或者,正则表达式也可以工作——更短,但效率更低:

    if (/onething |theotherthing /.test(this.state.users[id].items)){
    

    您可以使用对象表示法:

    if (this.state.users[id].items in {"onething ":1, "theotherthing ":1}){
    
    或者,正则表达式也可以工作——更短,但效率更低:

    if (/onething |theotherthing /.test(this.state.users[id].items)){
    

    像我这样的第三个ide,但不适用于像我这样的JavaSciptHird ide,但不适用于javasciptor一个简单的正则表达式,但我相信他不想这样做这只适用于数组吗?您的答案似乎缺少
    .name
    部分。(我刚刚为这个问题编了一个例子,我正在寻找一个普遍适用的方法)@Life4ants,对,这个部分不见了,但是你知道这个模式。您可以将所有字符串放入数组中,并使用indexOf.或简单的正则表达式进行检查,但我相信他不想这样做。这是否仅适用于数组?您的答案似乎缺少
    .name
    部分。(我刚刚为这个问题编了一个例子,我正在寻找一个普遍适用的方法)@Life4ants,对,这个部分不见了,但是你知道这个模式。您可以将所有字符串放入数组并使用indexOf进行检查。我最喜欢这个答案,尽管它与@nina的答案基本相同,只是使用了新的ES6
    。includes
    。你能编辑一下这篇文章,向初学者解释一下这是做什么的,以及它是如何工作的吗?我会给它一个最好的答案,尽管它和@nina的答案基本上是一样的,只是用新的ES6
    。includes
    。你能编辑一下这篇文章,给初学者解释一下这是做什么的,以及它是如何工作的吗?我会试试看