javascript - Transforming js array such that a numeric value is added -


my base data..

[{         month: 'jan',         cat: 'a',         val: 20     },{         month: 'jan',         cat: 'b','         val: 5     },{         month: 'jan',         cat: 'c',         val: 10     },{         month: 'feb',         cat: 'a',         val: 30     },{         month: 'feb',         cat: 'b',         val: 10     },{         month: 'feb',         cat: 'c',         val: 20     }]; 

i transform following format..

[{             dim:'val'         x: 'jan',             nval: 20 //same base object val         },{             dim:'val'             x: 'jan',             nval: 25// 20+5 -> above nval value + base object val         },{             dim:'val'             x: 'jan',             nval: 35//25+10         },{             dim:'val'             x: 'feb',             val: 65//35+30         },{             dim:'val'             x: 'feb',             nval: 75         },{             dim:'val'             x: 'feb',             nval: 95         }]; 

above js array has property dim has fixed value 'val', property x has same value of base object's month. property nval such property in first object of array has same value of base object's val. after base object's val get's added nval of prev object in new array.

i hope have been able explain need.. please ask if have doubts/ questions..

the new javascript array should able create using map how create nval val..

any sincerely appreciated..

thanks

hope you.

var newobject=[];  var tempdate=0;  var base =[{          month: 'jan',          cat: 'a',          val: 20      },{          month: 'jan',          cat: 'b',          val: 5      },{          month: 'jan',          cat: 'c',          val: 10      },{          month: 'feb',          cat: 'a',          val: 30      },{          month: 'feb',          cat: 'b',          val: 10      },{          month: 'feb',          cat: 'c',          val: 20      }];    for(var prop in base)  {    newobject.push({      'dim':'val',      'x':base[prop]['month'],      'nval':tempdate + base[prop]['val']    });    tempdate=tempdate + base[prop]['val'];  }    console.log(newobject);


Comments

Popular posts from this blog

ios - Is 'init' forbidden as *part* of a variable name? -

javascript - Why Selenium can't find an element that is graphically visible -

angular - Angular2 Router: Cannot find primary outlet to load 'HomeComponent' -