if数组

时间:2016-07-08 14:24:38

标签: javascript arrays

我有以下数组:

array = ["ProgA", "ProgC", "ProgG"]

此数组可能会根据用户输入而改变。

我有以下Json文件:

    {"ABC":{

        "ProgA": 1, 
        "ProgB": 0, 
        "ProgC": 1,  
        "ProgD": 0, 
        "ProgE": 0, 
        "ProgF": 1, 
        "ProgG": 1, 
        "ProgH": 0 


    },
    "DEF":{

        "ProgA": 1, 
        "ProgB": 0, 
        "ProgC": 0,  
        "ProgD": 0, 
        "ProgE": 1, 
        "ProgF": 0, 
        "ProgG": 1, 
        "ProgH": 0 


    },
    "GHI":{

        "ProgA": 1, 
        "ProgB": 1, 
        "ProgC": 1,  
        "ProgD": 1, 
        "ProgE": 1, 
        "ProgF": 1, 
        "ProgG": 1, 
        "ProgH": 1


    },
    "JKL":{

        "ProgA": 1, 
        "ProgB": 0, 
        "ProgC": 1,  
        "ProgD": 1, 
        "ProgE": 0, 
        "ProgF": 1, 
        "ProgG": 0, 
        "ProgH": 1 


    },
    "MNO":{

        "ProgA": 1, 
        "ProgB": 1, 
        "ProgC": 1,  
        "ProgD": 0, 
        "ProgE": 1, 
        "ProgF": 1, 
        "ProgG": 1, 
        "ProgH": 1

    }}

我的目标是基本上返回所有具有ProgA,ProgC和ProgG == 1的名称(“ABC”,“DEF”等)

当条件在可以更改的数组中时,我不确定如何评估if语句。

3 个答案:

答案 0 :(得分:10)

您可以使用filter()every()

执行此操作

var array = ["ProgA", "ProgC", "ProgG"];
var obj = {"ABC":{"ProgA":1,"ProgB":0,"ProgC":1,"ProgD":0,"ProgE":0,"ProgF":1,"ProgG":1,"ProgH":0},"DEF":{"ProgA":1,"ProgB":0,"ProgC":0,"ProgD":0,"ProgE":1,"ProgF":0,"ProgG":1,"ProgH":0},"GHI":{"ProgA":1,"ProgB":1,"ProgC":1,"ProgD":1,"ProgE":1,"ProgF":1,"ProgG":1,"ProgH":1},"JKL":{"ProgA":1,"ProgB":0,"ProgC":1,"ProgD":1,"ProgE":0,"ProgF":1,"ProgG":0,"ProgH":1},"MNO":{"ProgA":1,"ProgB":1,"ProgC":1,"ProgD":0,"ProgE":1,"ProgF":1,"ProgG":1,"ProgH":1}}

var result = Object.keys(obj).filter(function(e) {
  return array.every(function(a) {
    return obj[e][a] == 1;
  });
});

console.log(result)

答案 1 :(得分:3)

老派方法:遍历你的JSON对象,遍历你的数组输入,使用一个标志来检查它们是否匹配:

var arr = ["ProgA", "ProgC", "ProgG"]
var o = {"ABC":{"ProgA":1,"ProgB":0,"ProgC":1,"ProgD":0,"ProgE":0,"ProgF":1,"ProgG":1,"ProgH":0},"DEF":{"ProgA":1,"ProgB":0,"ProgC":0,"ProgD":0,"ProgE":1,"ProgF":0,"ProgG":1,"ProgH":0},"GHI":{"ProgA":1,"ProgB":1,"ProgC":1,"ProgD":1,"ProgE":1,"ProgF":1,"ProgG":1,"ProgH":1},"JKL":{"ProgA":1,"ProgB":0,"ProgC":1,"ProgD":1,"ProgE":0,"ProgF":1,"ProgG":0,"ProgH":1},"MNO":{"ProgA":1,"ProgB":1,"ProgC":1,"ProgD":0,"ProgE":1,"ProgF":1,"ProgG":1,"ProgH":1}}

var out = [];
for(var i in o){                        // loop over the object
  var good = true;                      // set the flag
  for(var a = 0; a < arr.length; a++){  // loop over the input array
    if (o[i][arr[a]] != 1) {            // check if it doesn't match
      good = false;                     // if so, unset the flag
      break;                            // and break the inner loop
    }
  }
  if (good) out.push(i);     // if the flag is set, we have a match
}

console.log(out);

答案 2 :(得分:-1)

var mainobj =  {"ABC":{

    "ProgA": 1, 
    "ProgB": 0, 
    "ProgC": 1,  
    "ProgD": 0, 
    "ProgE": 0, 
    "ProgF": 1, 
    "ProgG": 1, 
    "ProgH": 0 


},
"DEF":{

    "ProgA": 1, 
    "ProgB": 0, 
    "ProgC": 0,  
    "ProgD": 0, 
    "ProgE": 1, 
    "ProgF": 0, 
    "ProgG": 1, 
    "ProgH": 0 


},
"GHI":{

    "ProgA": 1, 
    "ProgB": 1, 
    "ProgC": 1,  
    "ProgD": 1, 
    "ProgE": 1, 
    "ProgF": 1, 
    "ProgG": 1, 
    "ProgH": 1


},
"JKL":{

    "ProgA": 1, 
    "ProgB": 0, 
    "ProgC": 1,  
    "ProgD": 1, 
    "ProgE": 0, 
    "ProgF": 1, 
    "ProgG": 0, 
    "ProgH": 1 


},
"MNO":{

    "ProgA": 1, 
    "ProgB": 1, 
    "ProgC": 1,  
    "ProgD": 0, 
    "ProgE": 1, 
    "ProgF": 1, 
    "ProgG": 1, 
    "ProgH": 1

}}

var result = [];

for(var obj in mainobj){
  console.log(mainobj[obj]['ProgA'])
  if(mainobj[obj]['ProgA'] && mainobj[obj]['ProgC'] && mainobj[obj]['ProgG']){
   result.push(obj);
}
 }

console.log(result);
console.log(result);