If I have data that looks like this
const reqObjects = {
"VAV1": "read 12345:2 analogInput 2",
"VAV2": "read 12345:3 analogInput 1",
"VAV3": "read 12345:4 analogInput 1",
"VAV4": "read 12345:5 analogInput 2",
"VAV5": "read 12345:6 analogInput 1",
"VAV6": "read 12345:7 analogInput 2",
"VAV7": "read 12345:8 analogInput 1",
"VAV8": "read 12345:9 analogInput 1",
"VAV9": "read 12345:10 analogInput 2",
"VAV10": "read 12345:11 analogInput 1"
}
How could I loop through this by feeding this data into a function, parse keys into string values, and use conditional logic to verify each of key values seem appropriate?
Dumbing this down a little bit for one string:
var myVar = "read 12345:2 analogInput 2";
I can split this into pieces:
var myVarParts = myVar.split(" ");
And assign variables to each piece:
requestType = myVarParts[0];
console.log(requestType)
deviceAddress = myVarParts[1];
console.log(deviceAddress)
pointType = myVarParts[2];
console.log(pointType)
pointAddress = myVarParts[3];
console.log(pointAddress)
Trying to make up some conditional logic if the requestType is good if its a read, write, release:
if(requestType === "read"){
console.log("Good on read");
}else if(requestType === "write"){
console.log("Good on write");
}else if(requestType === "release"){
console.log("Good on release");
}else{
console.log("BAD requestType");
}
The pointType is good if its a analogInput, analogValue, binaryValue, binaryInput.
Could someone give me a tip on what a Boolean JavaScript function would look like that could loop through this data and verify the keys look OK?
const reqObjects = {
"VAV1": "read 12345:2 analogInput 2",
"VAV2": "read 12345:3 analogInput 1",
"VAV3": "read 12345:4 analogInput 1",
"VAV4": "read 12345:5 analogInput 2",
"VAV5": "read 12345:6 analogInput 1",
"VAV6": "read 12345:7 analogInput 2",
"VAV7": "read 12345:8 analogInput 1",
"VAV8": "read 12345:9 analogInput 1",
"VAV9": "read 12345:10 analogInput 2",
"VAV10": "read 12345:11 analogInput 1"
}
Thank you I am trying to learn JavaScript.
Object.keys(theObject)or usingfor-in, then calling the validation function for each entry at that key. If you don't care about the keys you can getObject.values(theObject).