如何在JavaScript中动态设置if /或条件?

时间:2018-03-16 16:47:36

标签: javascript google-apps-script google-sheets

我对如何做到这一点有一个模糊的想法,但我被卡住了。如果有人可以建议我是否朝着正确的方向或者我应该尝试别的东西,我将不胜感激吗?

但是从外行的角度来看,我正在考虑这个问题,如果数组满足像(array[0][2] == 'default' || array[0][2] == 'thailand')这样的条件,我将获得一个新的[true, false, false, true, ...]数组,然后使用新的true / false数组,我会如果它是假的,可以用它来根据索引过滤数组吗?

array = array.filter(function(e) { 
  if ( array[0][2] == 'default' ||  array[0][2] == 'thailand' ) { 
    return e 
  } 
})

以下是更多详情:

 var array = [
    [9117.0, 5184.0, default, Chair, 0.0, th, in|ae, false, men], 
    [9117.0, 5184.0, indonesia, television, 0.0, id, in|ae, false, men], 
    [9117.0, 5184.0, indonesia, television, 82.0, sg, in|ae, false, men], 
    [9431.0, 5456.0, default, television, 0.0, au, , false, men], 
    [9431.0, 5456.0, default, television, 0.0, au, , true, men], 
    [9431.0, 5456.0, default, television, 0.0, th, , false, men], 
    [9431.0, 5456.0, default, television, 0.0, th, , true, men], 
    [9431.0, 5456.0, default, television, 94.0, sg, , true, men], 
    [9431.0, 5456.0, default, television, 0.0, id, , false, men], 
    [9431.0, 5456.0, default, television, 0.0, id, , true, men], 
    [9431.0, 5456.0, thailand, television, 94.0, sg, , false, men], 
    [9152.0, 5225.0, default, drawer, 0.0, th, in|ae, false, men], 
    [9152.0, 5225.0, default, television, 70.0, sg, in|ae, false, men], 
    [9152.0, 5225.0, default, television, 0.0, id, in|ae, false, men], 
    [9152.0, 5225.0, default, television, 4.0, au, in|ae, false, men], 
    [9221.0, 5335.0, indonesia, television, 0.0, th, in|ae, false, men], 
    [9221.0, 5335.0, indonesia, television, 2.0, au, in|ae, false, men], 
    [9221.0, 5335.0, default, television, 0.0, id, in|ae, false, men], 
    [9221.0, 5335.0, default, drawer, 0.0, sg, in|ae, false, men]
  ]

我是否需要一个带阵列的阵列(filter1&& filter2&& filter3)?

var filter1= [['default'], ['thailand']] //if ( array[0][2] == 'default' ||  array[0][2] == 'thailand' ) => true
var filter2 = [['television']] //if ( array[0][3] == 'television' ) =>true
var filer3 = [['true']] //if ( array[0][7] == 'true' ) => true

预期答案:

var answer = [
  [9431.0, 5456.0, default, television, 0.0, au, , true, men], 
  [9431.0, 5456.0, default, television, 0.0, th, , true, men], 
  [9431.0, 5456.0, default, television, 94.0, sg, , true, men], 
  [9431.0, 5456.0, default, television, 0.0, id, , true, men], 
  [9431.0, 5456.0, thailand, television, 94.0, sg, , true, men], 
]

2 个答案:

答案 0 :(得分:0)

在过滤器中,您需要使用参数,而不是数组本身。

此外,您需要返回true或false,而不是elemnts。像这样:

array = array.filter(function(e) { 
    if ( e[2] == 'default' ||  e[2] == 'indonesia' ) { 
       return true;
    }
    return false;
})

答案 1 :(得分:0)

做这样的事情:

首先,定义你的过滤器(注意,过滤器是返回true \ false的函数)

var filter1 = obj => obj[0][2] === 'default' || obj[0][2] === 'thailand';
var filter2 = obj => // do the filter
var filter3 = obj => // do the filter

var filters = [filter1, filter2, filter3]

然后,当您过滤所预测的数据时,请执行以下操作:

array.filter(x => filters.every(f => f(x))

在这种方法中,您的代码看起来会更简单,添加\删除过滤器会更容易,您可以轻松地进行一些更改。

祝你好运!