Monday, 15 September 2014

javascript - Cherry-picking one JSON object to create multiple arrays -


I have a JSON array with thirty objects (in the last 30 days of today), each object has these properties:

  { "on": "2013-05-20", "notCachedRequestsDevelopment": "115,482" "cachedRequestsDevelopment": "4,732,914", "notCachedBandwidthDevelopment": "15525231867", "Kasdbandvidth Development": " 2571078929 "," rejected disclaimer development ":" 44068 "," reject Krenvindvidth development ":" 23169212 "," non-CSS Kveshik Request development ":" 678 9 "," non-CSS Not Kachard Request development ":" 14 40 "," Notified Securities Production ":" 9 "", "Cash" CDNCachedRequests ": 6062986," CDNNotCachedRequests ":" 272901.0 "," CDNRejectRequests ":" 84764.0 "," CDNailBandwidth ": 56006050473.574," billingBandwidth ": 22525362831.36 , "billingHits": 6,489,103}   

I need to take this JSON and create a new array. for example:

I totalBandwidth need a new array that takes each JSON object and lowers the following properties: notCachedBandwidthDevelopment + cachedBandwidthDevelopment + rejected Krenbandvidth development No Kasdbandvi Dthutpadn + Kasdbandvidthprodkshn + rejected Krenbandvidthprodkshn

I have an array that developmentBandwidth and gets the sum of each object: cachedBandwidthDevelopment + NotCachedBandwidthDevelopment

one ?? | And so on.

I can do this with the loop for each new array, but I suspect that there is a clever way to do this?

"itemprop =" text ">

option 1: Array.prototype.map ()

You can create new JavaScript Try the array.prototype.map () array function. In your case, you might want something like:

  on developmentBandwidth = origArray.map (function (obj) {return parseInt (obj.cachedBandwidthDevelopment) + parseInt (obj.notCachedBandwidthDevelopment);} );   

developmentBandwidth will be an array of numbers.

Note that this is only implemented in ECMAScript 5, and only available in modern browsers. MDN:

They provide a compatibility function that allows you to use the function on older browsers.


Option 2: jQuery.map ()

It seems that the jQuery library provides a similar function. Similar examples:

 may be obtained by  var Vicasbandvidth = $ Kmap (inducing function (Obije, index) {return Parasent (obj.cached BandwidthDevelopment) + parseInt (obj.notCachedBandwidthDevelopment) above is; });   

Compare between two options

No comments:

Post a Comment