4

I have the following array:

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

This array can change depending on the user input.

I have the following Json File:

    {"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

    }}

My goal is to basically return all the names ("ABC", "DEF" etc) which have the ProgA, ProgC and ProgG == 1

I am not sure how to evaluate the if statements when the conditions are in an array which can change.

5
  • console.log(obj.ABC[ array[1] ]) will show the value of the "ProgC" switch inside ABC (obj is the variable name of he json object) Commented Jul 8, 2016 at 14:28
  • Loop over the object. Look over the array, look at value in object, see if all values equal to one, if true, you add it to an array Commented Jul 8, 2016 at 14:29
  • I am not sure I understand what you mean, but, you access the array same way as you would when they could not change. That is why it is an array to allow it. Commented Jul 8, 2016 at 14:32
  • @Dellirium I should have been more clear. Even if array doesn't change, i am not sure how to proceed. suppose i only had to return say all the things with `ProgA == 1", i can write a simple if loop to check but if there are multiple things to check, I am not sure how to do it. Commented Jul 8, 2016 at 14:35
  • @you would have to check within the loop, like this: jsfiddle.net/0mwpxpc6 Commented Jul 8, 2016 at 14:56

3 Answers 3

10

You can do this with filter() and 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)

Sign up to request clarification or add additional context in comments.

5 Comments

+1 to this answer for using .keys, .filter and .every, that's the cleanest approach in my opinion.
@briosheje Thanks :)
@NenadVracar Worked like charm. You are a genius.
@NenadVracar any idea how would the code change if it were OR. I mean return the name if any of the progA, ProgC, ProgG is == 1?
@Morpheus Yes, instead of every you can use some
3

Old-school approach: Loop over your JSON object, loop over your array input, use a flag to check whether they all match or not:

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

1 Comment

@naveen, haha, identical! :)
-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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.