I've been grappling with counting occurrences of property values in an array containing multiple objects. I am currently working on developing an efficient method to iterate through these values. The structure of the array might resemble this:
var arr=[{1:["option1","option2"],2:["option2"],3:["option1"]},{1:["option2","option1"],2:["option1"],3:["option1"]}]
The array can consist of an unknown number of objects. Each object's property may also contain an unknown number of values stored in an array. All objects share the same properties, so I attempted to consolidate all properties with the first object. Although functional, it appears clunky and difficult to read. This approach feels unstable and unpredictable.
for(var firstobj=0,val=0,i=1,prop=1;arr.length>i;prop++,val++)
{
if(prop>Object.getOwnPropertyNames(arr[i]).length)
{
i++;
prop=1;
}
else if(prop<Object.getOwnPropertyNames(arr[i]).length)
{
i+0;
}
if(arr[i][prop].length<=val)
{
val=0;
}
arr[firstobj][prop].push(arr[i][prop][val]);
}
In this code snippet, I attempt to manipulate the iteration by advancing to the next array index (i) only when the object's properties [prop] reach the end. I control the internal array iteration by resetting val to 0 if it exceeds the length of the array. Despite its functionality, I am dissatisfied with this piece of code. Perhaps you could offer alternative solutions to this issue. How can I organize multiple object values more effectively to later tally their occurrences for each property? Thank you.