javascript - complex java-script manipulation, array object sorting for new object -
i have array of objects need reshape 1 other work. need manipulation convert 1 function. have created plunker https://jsbin.com/himawakaju/edit?html,js,console,output
main factors month, country , "ac" value.
var actual = [ {"country":"uk","month":"jan","sr":"john p","ac":"24","pr":"2","tr":1240}, {"country":"austria","month":"jan","sr":"brad p","ac":"64","pr":"12","tr":1700}, {"country":"italy","month":"jan","sr":"gim p","ac":"21","pr":"5","tr":900}, {"country":"uk","month":"feb","sr":"john p","ac":"14","pr":"4","tr":540}, {"country":"austria","month":"feb","sr":"brad p","ac":"24","pr":"12","tr":1700}, {"country":"italy","month":"feb","sr":"gim p","ac":"22","pr":"3","tr":600}, {"country":"uk","month":"mar","sr":"john p","ac":"56","pr":"2","tr":1440}, {"country":"austria","month":"mar","sr":"brad p","ac":"24","pr":"12","tr":700}, {"country":"italy","month":"mar","sr":"gim p","ac":"51","pr":"5","tr":200} ]; var expect = [ {month:"jan",val: {"uk":"24","austria":"64","italy":"21"}}, {month:"feb",val: {"uk":"14","austria":"24","italy":"22"}}, {month:"mar",val: {"uk":"56","austria":"24","italy":"51"}} ];
var actual = [{ "country": "uk", "month": "jan", "sr": "john p", "ac": "24", "pr": "2", "tr": 1240 }, { "country": "austria", "month": "jan", "sr": "brad p", "ac": "64", "pr": "12", "tr": 1700 }, { "country": "italy", "month": "jan", "sr": "gim p", "ac": "21", "pr": "5", "tr": 900 }, { "country": "uk", "month": "feb", "sr": "john p", "ac": "14", "pr": "4", "tr": 540 }, { "country": "austria", "month": "feb", "sr": "brad p", "ac": "24", "pr": "12", "tr": 1700 }, { "country": "italy", "month": "feb", "sr": "gim p", "ac": "22", "pr": "3", "tr": 600 }, { "country": "uk", "month": "mar", "sr": "john p", "ac": "56", "pr": "2", "tr": 1440 }, { "country": "austria", "month": "mar", "sr": "brad p", "ac": "24", "pr": "12", "tr": 700 }, { "country": "italy", "month": "mar", "sr": "gim p", "ac": "51", "pr": "5", "tr": 200 }]; var monthsarray = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; function converter(actual) { var desiredarr = []; (var = 0; < monthsarray.length; ++i) { debugger var obj = {}; var innerobj = {}; var month = ''; (var j = 0; j < actual.length; ++j) { if (monthsarray[i] == actual[j].month.tolowercase()) { month = actual[j].month; innerobj[actual[j].country] = actual[j].ac; } } if (month.length > 0) { obj.month = month; obj.val = innerobj; desiredarr.push(obj); } } return desiredarr; } alert(json.stringify(converter(actual)));
Comments
Post a Comment