0

I want to subtract two values that I have in a hash array in order to know the growth I have of the Discard Value.

My array is this:

array = 
{"SiteA": [
 {device:"SiteA", discardmax:16617331511, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteA", discardmax:16617216094, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteA", discardmax:16617202279, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteA", discardmax:16616985649, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteA", discardmax:16616404836, timeCapturedmax:"February 18, 2018 at 09:07 AM"},  
 {device:"SiteA", discardmax:16616368250, timeCapturedmax:"February 18, 2018 at 09:03 AM"}
], 
"SiteB": [
 {device:"SiteB", discardmax:16617331511, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteB", discardmax:16617216094, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteB", discardmax:16617202279, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteB", discardmax:16616985649, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteB", discardmax:16616404836, timeCapturedmax:"February 18, 2018 at 09:07 AM"},  
 {device:"SiteB", discardmax:16616368250, timeCapturedmax:"February 18, 2018 at 09:03 AM"}
]}

I want to subtract the values from 'discardmax' and get the following array.

[
 {device:"SiteA", discardmax:115417, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteA", discardmax:13815, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteA", discardmax:216630, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteA", discardmax:580813, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteA", discardmax:36586, timeCapturedmax:"February 18, 2018 at 09:07 AM"},
 {device:"SiteB", discardmax:115417, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteB", discardmax:13815, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteB", discardmax:216630, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteB", discardmax:580813, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteB", discardmax:36586, timeCapturedmax:"February 18, 2018 at 09:07 AM"}
]

In ruby I used this code but now I want to use javascript:

  keystotal = arrya.keys

  arr_sub = keystotal.map { |k|
    [k, groupedtotal[k].each_cons(2).map do |g,h|
    { devicetotal: g[:devicetotal], porttotal: g[:porttotal], device_int_stats_total: g[:device_int_stats_total], octetsrxsub: g[:octetsrxtotal]-h[:octetsrxtotal], octetstxsub: g[:octetstxtotal]-h[:octetstxtotal], time_unixsub: g[:time_unixtotal]-h[:time_unixtotal], time_unixtotal: g[:time_unixtotal], timeCapturedtotal: g[:timeCapturedtotal]}
  end
  ]
  }
  table_sub = arr_sub.map { |ts| ts[1] }
1
  • That's not valid JavaScript. Use console.log(object, null, 2) to get the data in an appropriate format. Also, it's not clear what value you want to subtract. If you know the algorithm, please explain it as text, and try and write it as code. We can help you if you have problems with the last step. Commented May 11, 2020 at 18:28

1 Answer 1

1

Loop through the array, subtracting array[i+1].discardmax from array[i].discardmax. Create a new object that merges this into a copy of array[i].

var array = 
{"SiteA": [
 {device:"SiteA", discardmax:16617331511, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteA", discardmax:16617216094, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteA", discardmax:16617202279, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteA", discardmax:16616985649, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteA", discardmax:16616404836, timeCapturedmax:"February 18, 2018 at 09:07 AM"},  
 {device:"SiteA", discardmax:16616368250, timeCapturedmax:"February 18, 2018 at 09:03 AM"}
], 
"SiteB": [
 {device:"SiteB", discardmax:16617331511, timeCapturedmax:"February 18, 2018 at 09:27 AM"}, 
 {device:"SiteB", discardmax:16617216094, timeCapturedmax:"February 18, 2018 at 09:22 AM"}, 
 {device:"SiteB", discardmax:16617202279, timeCapturedmax:"February 18, 2018 at 09:18 AM"},
 {device:"SiteB", discardmax:16616985649, timeCapturedmax:"February 18, 2018 at 09:12 AM"}, 
 {device:"SiteB", discardmax:16616404836, timeCapturedmax:"February 18, 2018 at 09:07 AM"},  
 {device:"SiteB", discardmax:16616368250, timeCapturedmax:"February 18, 2018 at 09:03 AM"}
]};

var result = [];
for (var k in array) {
  var arr2 = array[k];
  for (var i = 0; i < arr2.length - 1; i++) {
    result.push({ ...arr2[i],
      discardmax: arr2[i].discardmax - arr2[i + 1].discardmax
    });
  }
}

console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I updated the question. Please your support.
Just add another loop around it that processes the properties of the top-level object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.