-2

This is how my array is set up:

"members" : [
{ "health" : 0,
  "stateStr" : "Not reachable", 
},

{ "health" : 1,
  "stateStr" : "PRIMARY",
},

{ "health" : 1,
  "stateStr" : "ARBITER",
}
],

What I'm trying to achieve is to iterate through all 3 members, and find if a member has stateStr = Primary (in the above example, member[1] will pass this condition). If that condition passes, look at the same member and see if health=1. In the above example, I would then look at if member[1].health =1. I'm not sure how to store that index of member.. I would appreciate it if you could show me exactly - I'm very new!

Here's the logic of it:

  • If any member has stateStr=Secondary. If yes, then print "Secondary node is still running." and break.
  • Else, Check if any member has stateStr="Primary". If Primary is found, check whether its health = 1
  • If none of the members are Primary, then print "No primary node found" and if Primary node is found but its health !=1, then print "Primary node unhealthy" and break.
  • Repeat the previous three steps for stateStr="Arbiter"

1 Answers1

1

Arrays in JavaScript can be iterated through with a for-loop.

for (var i = 0; i < members.length; i++) {
    if (members[i].stateStr == "PRIMARY") { // 1st condition
        if (members[i].health === 1) { // 2nd condition
            // store index i here
        }
    }
}

Understand that in the above for-loop, members[i] only gets the element containing the JSON object. We must then specify the JSON property after a period.

The way a JSON object works in this case is by indicating the JSON property name after a period for that element of the array. In simple terms, it works this:

members[i].JSON_PROPERTY

See this question and this on more about iterating through arrays with JSON. There is more than one way to do this too. This one is fairly common.

Lastly, remove the comma , at the end of the members array in the declaration. it should end with a ; since it's the end of the array declaration:

{ "health" : 1,
  "stateStr" : "ARBITER",
}
];
Paul
  • 137
  • 4
  • Can you help with how to tackle the logic of it? Here's the flowchart of the logic that I was following: -Check if any member has stateStr=Secondary. If yes, then print "Secondary node is still running." and break. -Check if any member has stateStr="Primary". if Primary is found, check whether its health = 1. Print ("success") -If none of the members are Primary, then print "No primary node found" and if Primary node is found but its health !=1, then print "Primary node unhealthy" and break. -Repeat the previous three steps for stateStr="Arbiter" – confusedreally Jan 15 '21 at 06:19
  • That's a fairly complicated set of logic. You're going to run into prioritization problems with what conditions stand superior to others. Maybe try writing it down first (in simple if's) to understand how it will work? It seems fairly long to walkthrough in this question. Get the logic down in terms of priority of each condition and ask another question and I'll try to help. – Paul Jan 15 '21 at 06:23
  • I've edited the question too with the logic! Its tough for me to wrap my head around the logic that I'm trying to achieve because in the for loop it will check every member.. I'll really appreciate it if you can write it out for me a little bit so I can understand. – confusedreally Jan 15 '21 at 06:23