I have a json file containing data as follows:
{ "available": { "scene00": 170, "scene01": 50, "scene02": 63, "scene03": 32, "scene04": 9, "scene05": 12 }, "original": { "scene00": 170, "scene01": 50, "scene02": 63, "scene03": 32, "scene04": 9, "scene05": 12 }
Where the number represents how many descriptions are available to be annotated in each scene.
I want to assign 20 descriptions for each user: 5 descriptions from 4 different scenes. I have 50 scenes in the original. Available is just to keep track of how many have been assigned so far.
I am not able to figure out how to recursively loop through all the original scenes and obtain the desired object.
Here's what my requirements are:
Create an object consisting of 20 description assignments.
Example:
"assigned": { "scene00": [1, 5], // assigned description 1 to 5 from 170 available. "scene01": [1, 5], "scene02": [1, 5], "scene03": [1, 5] }
So if a scene has less than 5 descriptions, it should pick remaining descriptions and go to next scene and check if the next scene has any descriptions and pick from it. As long as there are 20 descriptions at the end I'm good.
I also want to update available object by subtracting however many I assign from it until every field becomes 0. So, in the above example available object will be updated as:
{ "available": { "scene00": 165, // subtract assigned description from available and update it "scene01": 45, "scene02": 58, "scene03": 27, "scene04": 4, "scene05": 7 }
Finally, once every scene has no assignments left, it should reset the available object by deep copying the original object and starting over. The goal is to always end up with 20 descriptions, preferably from 4-5 different scenes.
Here's what I've tried so far:
// function to check how many descriptions have been assigned. function totalAssignedDescriptions(obj) { var sum = 0; for (var el in obj) { if (obj.hasOwnProperty(el)) { sum += parseFloat(obj[el][1] - obj[el][0] + 1); } } return sum; } let assignedDescriptions = {} let allScenes = Object.keys(available) let count = 0 let original = sceneDescriptionCount['original'] let available = sceneDescriptionCount['available'] while (totalAssignedDescriptions(assignSceneDescriptions) <= 20) { if (allScenes[count] != allScenes[allScenes.length - 1]) { if (available[allScenes[count]] >= 4) { let availableCount = original[allScenes[count]] - available[allScenes[count]] assignSceneDescriptions[allScenes[count]] = [availableCount + 1, availableCount + 4] available[allScenes[count]] -= 4 count += 1 } else if (available[allScenes[count]] < 4 && available[allScenes[count]] > 0) { let availableCount = original[allScenes[count]] - available[allScenes[count]] assignSceneDescriptions[allScenes[count]] = [availableCount + 1, original[allScenes[count]]] available[allScenes[count]] = 0 count += 1 } else { count += 1 } } else { // check if all other values are 0 then reset // else just increase count let remainingCount = [] for (let i = 0; i < allScenes.length; i++) { remainingCount.push(available[allScenes[i]]) } var allEqualToZero = remainingCount.every(function (value, index, array) { return value === 0; }); if (allEqualToZero) { Object.assign(available, original) count = 0 } else { // check if the last scene has descriptions available let i = 0 while (totalAssignedDescriptions(assignSceneDescriptions) <= 20) { if (remainingCount[i] != 0 && totalAssignedDescriptions(assignSceneDescriptions) < 20) { if (remainingCount[i] >= 4 && i < remainingCount.length - 1) { let availableCount = original[allScenes[i]] - remainingCount[i] assignSceneDescriptions[allScenes[i]] = [availableCount + 1, availableCount + 4] available[allScenes[i]] -= 4 i += 1 } else if (remainingCount[i] < 4 && remainingCount[i] > 0 && totalAssignedDescriptions(assignSceneDescriptions) < 20 && i < remainingCount.length - 1) { let availableCount = original[allScenes[i]] - remainingCount[i] assignSceneDescriptions[allScenes[i]] = [availableCount + 1, original[allScenes[i]]] available[allScenes[i]] = 0 i += 1 } } else { break } } } } }
https://stackoverflow.com/questions/65768651/creating-an-object-based-on-values-in-another-object January 18, 2021 at 11:58AM
没有评论:
发表评论