javascript - underscore - transform an array with _.map() -


i have multi-dimensional array of objects need transform different array. i'm convinced _.map() need. not having used before, i'm having trouble traversing array extract correct values. given simplified example:

[    {       "08/25/2015":[          {             "source":"somesource0",             "name":"somename0",             "stuff":"-6.728479",             "stuffvalue":"14.862200",             "amount":"-100.00"          },          {             "notneeded0":-100,             "subtotal":"-100.00"          }       ]    },    {       "08/26/2015":[          {             "source":"somesource1",             "name":"somename1",             "stuff":"-9.496676",             "stuffvalue":"10.530000",             "amount":"-100.00"          },          {             "notneeded0":-100,             "subtotal":"-100.00"          }       ]    },    {       "08/27/2015":[          {             "source":"somesource2",             "name":"somename2",             "stuff":"-9.469697",             "stuffvalue":"10.560000",             "amount":"-100.00"          },          {             "notneeded0":-100,             "subtotal":"-100.00"          }       ]    },    {       "08/28/2015":[          {             "source":"somesource3",             "name":"somename3",             "stuff":"-1.731841",             "stuffvalue":"10.570000",             "amount":"-18.24"          },          {             "source":"somesource4",             "name":"somename4",             "stuff":"-2.628939",             "stuffvalue":"31.100000",             "amount":"-81.76"          },          {             "notneeded0":-100,             "subtotal":"-100.00"          }       ]    },    {       "notneeded1":-400,       "notneeded2":"-400.00"    } ] 

i needed transform structured this:

[    {       "date":"08/27/2015",       "detail":[          {             "source":"somesource2",             "name":"somename2",             "stuff":"-9.469697",             "stuffvalue":"10.560000",             "amount":"-100.00",             "subtotal":"-100.00"          }       ]    },    {       "date":"08/28/2015",       "detail":[          {             "source":"somesource3",             "name":"somename3",             "stuff":"-1.731841",             "stuffvalue":"10.570000",             "amount":"-18.24",             "subtotal":"-100.00"          },          {             "source":"somesource4",             "name":"somename4",             "stuff":"-2.628939",             "stuffvalue":"31.100000",             "amount":"-81.76",             "subtotal":"-100.00"          }       ]    } ] 

please feel free ask questions if needed. assistance.

edit: code not web-based use. reference web , browser compatibility not necessary. and, use underscore library simplify tasks looping can pretty ugly using pure js. hope clarifies intent.

using built-in .filter, .map , .splice:

var modified = original.filter(function only_nested_arrays(obj) {   var keys = object.keys(obj)   return keys.length === 1 && array.isarray(obj[keys[0]]) }).map(function transform(obj) {   var date = object.keys(obj)[0],       inner_array = obj[date],       subtotal = inner_array.splice(-1)[0].subtotal   inner_array.foreach(function(obj_inner) {     obj_inner.subtotal = subtotal   })   return {date: date, detail: inner_array} })  console.log(json.stringify(modified, null, 2)) 

but watch out browser compatibility (see links polyfill make work on ie8 or lower). runnable code alerts result if want test:

"use strict"  var original = [    {      "08/25/2015":[        {          "source":"somesource0",          "name":"somename0",          "stuff":"-6.728479",          "stuffvalue":"14.862200",          "amount":"-100.00"        },        {          "notneeded0":-100,          "subtotal":"-100.00"        }      ]    },    {      "08/26/2015":[        {          "source":"somesource1",          "name":"somename1",          "stuff":"-9.496676",          "stuffvalue":"10.530000",          "amount":"-100.00"        },        {          "notneeded0":-100,          "subtotal":"-100.00"        }      ]    },    {      "08/27/2015":[        {          "source":"somesource2",          "name":"somename2",          "stuff":"-9.469697",          "stuffvalue":"10.560000",          "amount":"-100.00"        },        {          "notneeded0":-100,          "subtotal":"-100.00"        }      ]    },    {      "08/28/2015":[        {          "source":"somesource3",          "name":"somename3",          "stuff":"-1.731841",          "stuffvalue":"10.570000",          "amount":"-18.24"        },        {          "source":"somesource4",          "name":"somename4",          "stuff":"-2.628939",          "stuffvalue":"31.100000",          "amount":"-81.76"        },        {          "notneeded0":-100,          "subtotal":"-100.00"        }      ]    },    {      "notneeded1":-400,      "notneeded2":"-400.00"    }  ]    var modified = original.filter(function only_nested_arrays(obj) {    var keys = object.keys(obj)    return keys.length === 1 && array.isarray(obj[keys[0]])  }).map(function transform(obj) {    var date = object.keys(obj)[0],        inner_array = obj[date],        subtotal = inner_array.splice(-1)[0].subtotal    inner_array.foreach(function(obj_inner) {      obj_inner.subtotal = subtotal    })    return {date: date, detail: inner_array}  })    alert(json.stringify(modified, null, 2))


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -