Sum values from two arrays

I have two arrays:

allocatedHours: {{id:"", totalHours:12, week:fn!date(2020, 07, 31)}; {id:"", totalHours:12, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:48, week:fn!date(2020, 08, 21)}}

proposedHours: {{id:"", totalHours:36, week:fn!date(2020, 07, 31)}; {id:"", totalHours:42, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:54, week:fn!date(2020, 08, 21)}}

I am wanting to return TotalHours where allocatedHours.week = proposedHours.week.

When I do a forEach on weeks, it only ever returns the first value for each week, ie {31/7/2020, 48}, {7/8/2020, 48}, {14/8/2020,48}, {21/8/2020, 48).

How do I make if return the correct value for each week, ie {31/7/2020, 48}, {7/8/2020, 54}, {14/8/2020,96}, {21/8/2020, 102).

  Discussion posts and replies are publicly visible

Parents
  • I should point out that these are actually CDTs

    Allocated Hours

    Proposed Hours

  • 0
    Certified Senior Developer
    in reply to ireneb

    Hi,

    Please try and check if the below code works. I have created separate locals for Actual Hours and Proposed Hours. You can replace them as per your requirement.

    load(
      local!allocatedHours: {
        {
          id: "",
          totalHours: 12,
          week: fn!date(
            2020,
            07,
            31
          )
        };{
          id: "",
          totalHours: 12,
          week: fn!date(
            2020,
            08,
            7
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            14
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            21
          )
        }
      },
      local!proposedHours: {
        {
          id: "",
          totalHours: 36,
          week: fn!date(
            2020,
            07,
            31
          )
        };{
          id: "",
          totalHours: 42,
          week: fn!date(
            2020,
            08,
            7
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            14
          )
        };{
          id: "",
          totalHours: 54,
          week: fn!date(
            2020,
            08,
            21
          )
        }
      },
      local!checkIfAllocatedAndPropEqual: a!forEach(
        items: local!allocatedHours,
        expression: local!allocatedHours.week[fv!index] = local!proposedHours.week[fv!index]
      ),
      local!sumIfEqual:a!forEach(
        items:local!checkIfAllocatedAndPropEqual,
        expression:if(
          local!checkIfAllocatedAndPropEqual[fv!index],
        concat(local!allocatedHours.week[fv!index]," , ",sum(
          tointeger(local!allocatedHours.totalHours[fv!index])+
          tointeger(local!proposedHours.totalHours[fv!index]))),
          {})
      ),
      local!sumIfEqual
    )

Reply
  • 0
    Certified Senior Developer
    in reply to ireneb

    Hi,

    Please try and check if the below code works. I have created separate locals for Actual Hours and Proposed Hours. You can replace them as per your requirement.

    load(
      local!allocatedHours: {
        {
          id: "",
          totalHours: 12,
          week: fn!date(
            2020,
            07,
            31
          )
        };{
          id: "",
          totalHours: 12,
          week: fn!date(
            2020,
            08,
            7
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            14
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            21
          )
        }
      },
      local!proposedHours: {
        {
          id: "",
          totalHours: 36,
          week: fn!date(
            2020,
            07,
            31
          )
        };{
          id: "",
          totalHours: 42,
          week: fn!date(
            2020,
            08,
            7
          )
        };{
          id: "",
          totalHours: 48,
          week: fn!date(
            2020,
            08,
            14
          )
        };{
          id: "",
          totalHours: 54,
          week: fn!date(
            2020,
            08,
            21
          )
        }
      },
      local!checkIfAllocatedAndPropEqual: a!forEach(
        items: local!allocatedHours,
        expression: local!allocatedHours.week[fv!index] = local!proposedHours.week[fv!index]
      ),
      local!sumIfEqual:a!forEach(
        items:local!checkIfAllocatedAndPropEqual,
        expression:if(
          local!checkIfAllocatedAndPropEqual[fv!index],
        concat(local!allocatedHours.week[fv!index]," , ",sum(
          tointeger(local!allocatedHours.totalHours[fv!index])+
          tointeger(local!proposedHours.totalHours[fv!index]))),
          {})
      ),
      local!sumIfEqual
    )

Children