Need help troubleshooting an if statement inside a function that is called by a forEach array loop.
My array contains objects, with each object structured like this:
arrofobj = [
{"thing_id":"1a", "val": 1, "Type": "Switch","ValType":{"0":"Open","1":"Closed"}},
{"thing_id":"1b", "val": 72, "Type": "Sensor","ValType":{"0":"%"}}]
I want to check if the Type
is a switch, and if true, I need to populate a new field in the objects of the array CatX
:
- If it is a switch, I will use the val
value to determine which ValType
element to include in a new variable of the array arrofobj
.
- If it is not a switch, I will use the arrofobj.ValType.0
value.
const getCat = function(){
if(arrofobj.Type !== 'Switch')
arrofobj.ValType'.0'
} else {
arrofobj.ValType.(arrofobj.val)
};
arrofobj.forEach(p => p.CatX = getCat() );
I'm having trouble with the lint not accepting the code, so I am unable to test it.