JavaScript - add new properties to an object element if conditions are met

Currently I have data like this :

const boundaryOne = 150;
const boundaryTwo = 199;

let currentData = {
  total: 16,
  requests: [
    {
      category: 105,
      count: 1,
      actualValue: 109.70299999999999,
      percentageAmongTotal: 6.25,
    },
    { category: 115, count: 0, percentageAmongTotal: 0 },
    { category: 125, count: 0, percentageAmongTotal: 0 },
    { category: 135, count: 0, percentageAmongTotal: 0 },
    {
      category: 145,
      count: 2,
      actualValue: 149.607,
      percentageAmongTotal: 12.5,
    },
    {
      category: 145,
      count: 2,
      actualValue: 151.2,
      percentageAmongTotal: 12.5,
      displayTooltip: 'always',
      tooltipOrientation: 'down',
      tooltipDistance: -7,
    },
    { category: 155, count: 0, percentageAmongTotal: 0 },
    {
      category: 165,
      count: 1,
      actualValue: 169.49999999999997,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'down',
      tooltipDistance: -7,
    },
    { category: 175, count: 0, percentageAmongTotal: 0 },
    { category: 185, count: 0, percentageAmongTotal: 0 },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 205,
      count: 1,
      actualValue: 214.614,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    { category: 215, count: 0, percentageAmongTotal: 0 },
    {
      category: 225,
      count: 1,
      actualValue: 234.30651000000003,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    { category: 235, count: 0, percentageAmongTotal: 0 },
    { category: 245, count: 0, percentageAmongTotal: 0 },
    { category: 255, count: 0, percentageAmongTotal: 0 },
    { category: 265, count: 0, percentageAmongTotal: 0 },
    { category: 275, count: 0, percentageAmongTotal: 0 },
    {
      category: 285,
      count: 2,
      actualValue: 289.324,
      percentageAmongTotal: 12.5,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    {
      category: 285,
      count: 2,
      actualValue: 289.324,
      percentageAmongTotal: 12.5,
    },
  ],
};

I would like to add a couple of new properties (countExcludedRequests and percentageOfExcludedRequests) to an element :

  • whose count > 0
  • that has all of displayTooltip, tooltipOrientation, and tooltipDistance properties
  • that has other requests which share a same section

Let us have a look at currentData above. currentData.requests[5] have section value in common with currentData.requests[4] which is 145.

...
{
  category: 145,
  count: 2,
  actualValue: 149.607,
  percentageAmongTotal: 12.5,
},
{
  category: 145,
  count: 2,
  actualValue: 151.2,
  percentageAmongTotal: 12.5,
  displayTooltip: 'always',
  tooltipOrientation: 'down',
  tooltipDistance: -7,
},
...

Take into account that count and percentageAmongTotal do not belong to each request but to the category.
currentData.requests[4].actualValue = 149.607 and is therefore lower than boundaryOne. This should add the countExcludedRequests value.

Because one request has been excluded, set countExcludedRequests = 1 in currentData.requests[5].
In addition, add percentageOfExcludedRequests = (countExcludedRequests / currentData.total) * 100 into the same one.

After the process, the requests with category: 145 will look like this :

...
{
  category: 145,
  count: 2,
  actualValue: 149.607,
  percentageAmongTotal: 12.5,
},
{
  category: 145,
  count: 2,
  actualValue: 151.2,
  percentageAmongTotal: 12.5,
  displayTooltip: 'always',
  tooltipOrientation: 'down',
  tooltipDistance: -7,
  countExcludedRequests: 1,
  percentageOfExcludedRequests: 6.25
},
...

– Expected output –

The expected output is like following :

const expected = {
  requests: [
    {
      category: 105,
      count: 1,
      actualValue: 109.70299999999999,
      percentageAmongTotal: 6.25,
    },
    { category: 115, count: 0, percentageAmongTotal: 0 },
    { category: 125, count: 0, percentageAmongTotal: 0 },
    { category: 135, count: 0, percentageAmongTotal: 0 },
    {
      category: 145,
      count: 2,
      actualValue: 149.607,
      percentageAmongTotal: 12.5,
    },
    {
      category: 145,
      count: 2,
      actualValue: 151.2,
      percentageAmongTotal: 12.5,
      displayTooltip: 'always',
      tooltipOrientation: 'down',
      tooltipDistance: -7,
      countExcludedRequests: 1, // <- newly added
      percentageOfExcludedRequests: 6.25, // <- newly added
    },
    { category: 155, count: 0, percentageAmongTotal: 0 },
    {
      category: 165,
      count: 1,
      actualValue: 169.49999999999997,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'down',
      tooltipDistance: -7,
    },
    { category: 175, count: 0, percentageAmongTotal: 0 },
    { category: 185, count: 0, percentageAmongTotal: 0 },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 195,
      count: 8,
      actualValue: 200.86100000000002,
      percentageAmongTotal: 50,
    },
    {
      category: 205,
      count: 1,
      actualValue: 214.614,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    { category: 215, count: 0, percentageAmongTotal: 0 },
    {
      category: 225,
      count: 1,
      actualValue: 234.30651000000003,
      percentageAmongTotal: 6.25,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    { category: 235, count: 0, percentageAmongTotal: 0 },
    { category: 245, count: 0, percentageAmongTotal: 0 },
    { category: 255, count: 0, percentageAmongTotal: 0 },
    { category: 265, count: 0, percentageAmongTotal: 0 },
    { category: 275, count: 0, percentageAmongTotal: 0 },
    {
      category: 285,
      count: 2,
      actualValue: 289.324,
      percentageAmongTotal: 12.5,
      displayTooltip: 'always',
      tooltipOrientation: 'up',
      tooltipDistance: -7,
    },
    {
      category: 285,
      count: 2,
      actualValue: 289.324,
      percentageAmongTotal: 12.5,
    },
  ],
};

Any help is much appreciated

It looks like you have the algorithm decided, so have you tried implementing anything yet?

Thanks for your reply, my code currently looks like this : JavaScript - add new properties to an object element if conditions are met · GitHub

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.