My challenge involves analyzing an array of objects containing origin and destination data, and the total volume of objects moving between locations. I am specifically looking to compare flow counts between two cities. For example, when the origin is Vancouver and the destination is San Francisco, the count is 5, but when reversed, the count is 12.
InOut = [
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "New York, New York",
"count": 5
},
...
{
"origin": "San Francisco, California",
"dest": "Vancouver, Canada",
"count": 12
}
]
To extract counts using origins and destinations, I created a function:
function findValueByKey(array, key, value, key2, value2) {
...
}
var obj = findValueByKey(InOut, 'origin', 'Vancouver, Canada', 'dest', 'San Francisco, California');
var obj2 = findValueByKey(InOut, 'origin', 'San Francisco, California', 'dest', 'Vancouver, Canada');
console.log('obj', obj)
console.log('obj', obj2)
However, I am struggling to iterate through the array to retrieve all counts. My current attempt is:
allLocations = [...];
origDestValues = []
for(i=0; i< allLocations.length;i++){
InOutA = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
InOutB = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
...
};
While this approach is flawed as it looks for the same origin/destination repeatedly, I believe it's a step in the right direction. I wanted to demonstrate my efforts before seeking help here. Additionally, my actual array consists of many more cities which I have simplified for this query. Your assistance is greatly appreciated.