Simplifying a policy loop

https://gitgud.io/pregmodfan/fc-pregmod/-/blob/pregmod-master/src/interaction/policies/policies.js#L73

Question on this @svornost (not urgent, just practicing). I can't quite seem to clean this up. Most of these arrays have a single object and we always need to use that UNLESS there's more than one AND its enable property matches.

This is closer, I think:

const i = Object.keys(policyObject).findIndex(pol => policyObject[pol].hasOwnProperty("enable") && policyObject[pol].enable === policyValue) || 0;
const p = policyObject[i];

However, if findIndex fails it will return -1 so we never reach 0.

Confusingly, I think I made a mistake a while ago and policyObject is actually an array. That's an easier fix though.

edit:

const i = policyArray.findIndex(pol => pol.hasOwnProperty("enable") && pol.enable === policyValue) || 0;
const p = policyArray[i];

ugh: I think I got it. Figuring out the missnamed array was the key.

const p = policyArray.find(pol => pol.hasOwnProperty("enable") && pol.enable === policyValue) || policyArray[0];
Edited by lowercasedonkey