I have a tool I’m working on which is a essentially a collection of “rules” and “actions”.
Users create one or more rules, and when all the rules are true, one or more actions are performed.
I’m storing all the rules and actions in a single object tree.
When processing all the rules and actions, there is obviously some object traversing I have to do to list everything out and process them in very specific order, so indexed locations in the object are also important.
A very simplified version of the object currently looks like this:
var myRules = [
{
name: "My first rule",
rules: [
{ ... }, // rule 1
{ ... }, // rule 2
{ ... } // rule 3
],
actions: [
{ ... }, // action 1
{ ... } // action 2
]
},
{
// The next set of rules and actions here
}
]
Note that the main myRules variable is set up as an array from the start. This is because each set of rules and actions doesn’t really need to have a key, it’s just an array of objects.
My program has to traverse myRules to process each ruleset and actions. I’ll be accessing these via their indexed number in a loop structure, and in the order they appear.
So my question is this; is it perfectly valid to create myRules as an array here? Or would it be preferred to have this as an object? Are arrays generally avoided in favor of objects in most cases where there is a choice?
I know you can do slightly fancier things with objects perhaps, but because I want to traverse and loop over these, the arrays make more sense.
Also note that this object doesn’t have to store exportable functions, nor does it have any methods built in, it’s more of a structure just for storing settings/data.
That’s it in a nutshell. Should I avoid using array and force everything into object syntax? Or is there nothing wrong with arrays for this type of use case? Am I missing out on some future abilities, or missing out on some advanced features of objects for scale or future-proofing? Any reason I should try to use objects instead of arrays here?