Howard Shaw
Howard Shaw

Reputation: 1071

Obtaining totals from properties of parallel arrays in nested objects

I'm trying to obtain totals from properties that exist in objects in 'parallel' nested arrays. The data describes exercise sets.

I'd like to populate the "totalWeight" fields with the sum of weight lifted for each exercise. The data structure is below. The total should be the "setNum" of the "reps" values x the "setNum" of the "Weight" values where the setNums exist in two parallel arrays. So for the first exercise (Dumbbell Shoulder press) the totalWeight should be 560 ((8 reps x 28 weight)+(6x28)+(6x28)). The measurement types are distinguished by the "mu_id" property from where the "key" property value is obtained.

A complicating factor is that some exercises might have left-hand & right-hand reps so there are 3 parallel arrays as in the second example of the One Arm Cable Front Raise. There, the totalWeight should be 265 ((8 lh reps + 8 rh reps )*6.25 weight)+((7+8)*6.25)+((10+9)*3.75)).

The data structure was created by a d3.js nest routine from a flat database file but I haven't figured out how I can get these values either as part of the nesting (using rollup) or otherwise. I have access to d3.js but no other js libraries.

[
   {
      "exercise":"Dumbbell Shoulder press",
      "totalWeight":0,
      "values":[
         {
            "key":"1",
            "values":[
               {
                  "resultsId":10381,
                  "setNum":"8",
                  "desc":"times",
                  "unit":"Reps",
                  "mu_id":1
               },
               {
                  "resultsId":10382,
                  "setNum":"6",
                  "desc":"times",
                  "unit":"Reps",
                  "mu_id":1
               },
               {
                  "resultsId":10383,
                  "setNum":"6",
                  "desc":"times",
                  "unit":"Reps",
                  "mu_id":1
               }
            ]
         },
         {
            "key":"3",
            "values":[
               {
                  "resultsId":10381,
                  "setNum":"28",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               },
               {
                  "resultsId":10382,
                  "setNum":"28",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               },
               {
                  "resultsId":10383,
                  "setNum":"28",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               }
            ]
         }
      ]  
   },
   {
      "exercise":"One Arm Cable Front Raise",
      "totalWeight":0,
      "values":[
         {
            "key":"3",
            "values":[
               {
                  "resultsId":10395,
                  "setNum":"6.25",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               },
               {
                  "resultsId":10396,
                  "setNum":"6.25",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               },
               {
                  "resultsId":10397,
                  "setNum":"3.75",
                  "desc":"kilograms",
                  "unit":"Weight",
                  "mu_id":3
               }
            ]
         },
         {
            "key":"7",
            "values":[
               {
                  "resultsId":10395,
                  "setNum":"8",
                  "desc":"times",
                  "unit":"Reps (left)",
                  "mu_id":7
               },
               {
                  "resultsId":10396,
                  "setNum":"7",
                  "desc":"times",
                  "unit":"Reps (left)",
                  "mu_id":7
               },
               {
                  "resultsId":10397,
                  "setNum":"10",
                  "desc":"times",
                  "unit":"Reps (left)",
                  "mu_id":7
               }
            ]
         },
         {
            "key":"8",
            "values":[
               {
                  "resultsId":10395,
                  "setNum":"8",
                  "desc":"times",
                  "unit":"Reps (right)",
                  "mu_id":8
               },
               {
                  "resultsId":10396,
                  "setNum":"8",
                  "desc":"times",
                  "unit":"Reps (right)",
                  "mu_id":8
               },
               {
                  "resultsId":10397,
                  "setNum":"9",
                  "desc":"times",
                  "unit":"Reps (right)",
                  "mu_id":8
               }
            ]
         }
      ]
   },
   {...}
]

Upvotes: 1

Views: 45

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386680

You could collect the wanted data in an array and reduce the inner arrays and then add the single values.

var data = [{ exercise: "Dumbbell Shoulder press", totalWeight: 0, values: [{ key: "1", values: [{ resultsId: 10381, setNum: "8", desc: "times", unit: "Reps", mu_id: 1 }, { resultsId: 10382, setNum: "6", desc: "times", unit: "Reps", mu_id: 1 }, { resultsId: 10383, setNum: "6", desc: "times", unit: "Reps", mu_id: 1 }] }, { key: "3", values: [{ resultsId: 10381, setNum: "28", desc: "kilograms", unit: "Weight", mu_id: 3 }, { resultsId: 10382, setNum: "28", desc: "kilograms", unit: "Weight", mu_id: 3 }, { resultsId: 10383, setNum: "28", desc: "kilograms", unit: "Weight", mu_id: 3 }] }] }, { exercise: "One Arm Cable Front Raise", totalWeight: 0, values: [{ key: "3", values: [{ resultsId: 10395, setNum: "6.25", desc: "kilograms", unit: "Weight", mu_id: 3 }, { resultsId: 10396, setNum: "6.25", desc: "kilograms", unit: "Weight", mu_id: 3 }, { resultsId: 10397, setNum: "3.75", desc: "kilograms", unit: "Weight", mu_id: 3 }] }, { key: "7", values: [{ resultsId: 10395, setNum: "8", desc: "times", unit: "Reps (left)", mu_id: 7 }, { resultsId: 10396, setNum: "7", desc: "times", unit: "Reps (left)", mu_id: 7 }, { resultsId: 10397, setNum: "10", desc: "times", unit: "Reps (left)", mu_id: 7 }] }, { key: "8", values: [{ resultsId: 10395, setNum: "8", desc: "times", unit: "Reps (right)", mu_id: 8 }, { resultsId: 10396, setNum: "8", desc: "times", unit: "Reps (right)", mu_id: 8 }, { resultsId: 10397, setNum: "9", desc: "times", unit: "Reps (right)", mu_id: 8 }] }] }];

data.forEach(function (o) {
    o.totalWeight = o.values.map(function (a) {
        return a.values.map(function (b) {
            return +b.setNum;
        });
    }).reduceRight(function (a, b, j) {
        return a.map(function (v, i) {
            return j ? v + b[i] : v * b[i];
        });
    }).reduce(function (a, b) {
        return a + b;
    });
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions