2016-07-08 54 views
4

指定語句條件我有以下的數組:如果在陣列

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 

    }} 

我的目標是基本返回所有的名字( 「ABC」, 「DEF」 等),它們具有PROGA,PROGC和ProgG == 1

我不知道如何評估if語句當條件是在一個可以改變的數組中。在你的JSON對象,循環遍歷的數組輸入,使用標誌,檢查是否他們都:

+0

'的console.log(obj.ABC [陣列[1]])'將顯示' 「PROGC」 的值''內部開關ABC'('obj'是他的變量名JSON對象) –

+0

在對象上循環。查看數組,查看對象中的值,查看是否所有值都等於1,如果爲true,則將其添加到數組中 – epascarello

+0

我不確定我是否理解您的意思,但是,您以同樣的方式訪問數組當他們無法改變時。這就是爲什麼它是一個允許它的數組。 – Dellirium

回答

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)

+4

+1對於使用'.keys','.filter'和'.every'這個答案,這是我認爲最乾淨的方法。 – briosheje

+0

@briosheje謝謝:) –

+0

@NenadVracar工作就像魅力。你是一個天才。 – Morpheus

3

老派的做法匹配與否:

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);

+0

舊學校:) https://jsfiddle.net/codeandcloud/n38d009n/ – naveen

+1

@naveen,哈哈,相同!:) – Shomz

-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); 
+0

這會失敗如此糟糕y一旦輸入發生變化...... – Shomz