0๐
const obj = {
"timesheets": [
{
"user": {
"username": "erik",
"first_name": "Erik",
},
"project_id": 4,
"calc_full_week": {
"2020-06-22": 5,
"2020-06-23": 10,
"2020-06-24": 8,
"2020-06-25": 13,
"2020-06-26": null,
"2020-06-27": null,
"2020-06-28": null
}
},
{
"user": {
"username": "erik",
"first_name": "Erik",
},
"project_id": 4,
"calc_full_week": {
"2020-06-29": 15,
"2020-06-30": 10,
"2020-07-01": null,
"2020-07-02": null,
"2020-07-03": null,
"2020-07-04": null,
"2020-07-05": null
}
},
{
"user": {
"username": "rawwe",
"first_name": "joey",
},
"project_id": 4,
"calc_full_week": {
"2020-06-22": 3,
"2020-06-23": 10.4,
"2020-06-24": 8,
"2020-06-25": 8,
"2020-06-26": 8,
"2020-06-27": 8,
"2020-06-28": 5
}
}
]
}
const sumOfValues = (firstDiaposon, secondDiaposon) => {
const diaposon1 = +firstDiaposon.split('-').join('')
const diaposon2 = +secondDiaposon.split('-').join('')
let totalSum = 0
obj.timesheets.reduce((acc, val) => {
Object.keys(val.calc_full_week).map((date, index) => {
const dateSplit = +date.split('-').join('')
if (dateSplit >= diaposon1 && dateSplit <= diaposon2) {
totalSum += val.calc_full_week[date]
}
})
}, 0)
return totalSum
}
console.log(sumOfValues('2020-06-25', '2020-07-03'))
0๐
One of options if you want to do this in python:
First, you will need a list of allowed dates, like
my_date_range = ['2020-06-25', '2020-06-26', ]
Which better to be generated by separate method (given start and end date), i.e. from this SO answer.
To easier extract required sub-nodes from nested dictionary / json you can use dpath:
import json
import dpath
data = json.loads(json_string) # parse json string to dict
# filter dict for all date values
# 2020-* - some initial key name filter, may just get all with *
# yielded=True - returns generator (for one time use) for tuples, not a dict
result = dpath.util.search(data, "timesheets/*/calc_full_week/2020-*", yielded=True)
# Now sum only required dates
# also, need to filter out possible None values
my_sum = sum(filter(None, [v for k, v in result if k in my_date_range]))
- [Vuejs]-Filtering table on the frontend โ Vue.JS
- [Vuejs]-How to create data binding with array input ( multiple input) on vue
Source:stackexchange.com