I'm trying to figure out top body locations with the following data that looks like this... the data is an array of objects which could be an n number of objects in the objects, there is a key called bodyLocations. bodyLocations is an array I need to count r-ovary, l-ovary etc and figure out which 3 appear the most out of all the objects.
Currently, my thought process is to contact all the body locations array together then count? I feel there is an easier way to do this. Should I put the heavier work on the mongodb which this data is coming from or do it on my server where I'm currently trying? Any thoughts on how to approach this?
I am data for the week: [
{
bodyLocations: [ 'r-ovary', 'l-ovary', 'l-leg', 'r-leg', 'head' ],
typePain: [ 'aching', 'fatigue' ],
_id: 6074d376215c0c033aa41aef,
postedBy: 6060ca6b8dbe9d02e4ad1063,
created: '2021-04-12',
symptomDate: 2021-04-12T00:00:00.000Z,
painlevel: 6,
__v: 0
},
{
bodyLocations: [ 'r-ovary', 'uterus' ],
typePain: [ 'fatigue', 'twisting', 'radiating' ],
_id: 6074dd89ebabd8034831786a,
postedBy: 6060ca6b8dbe9d02e4ad1063,
created: '2021-04-12',
symptomDate: 2021-04-12T00:00:00.000Z,
painlevel: 9,
__v: 0
}
This is not collecting data in bigArr
:
el.bodyLocations.push(bigArr)
Instead it pushes an (empty) bigArr
reference to each object's bodyLocations
property.
But if you are interested in the frequency of occurrences, then do this:
const freq = {};
for (let {bodyLocations} of symptomDataWeek) {
for (let location of bodyLocations) freq[location] = (freq[location] || 0) + 1;
}
And then get the location with the greatest frequency:
const maxFreq = Math.max(...Object.values(freq));
const location = Object.keys(freq).find(location => freq[location] === maxFreq);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.