JavaScript to remove duplicate objects in an Object Array & sum the properties -
i've been trying days come solution problem , can't, imagine have next json array (we shall phone call jsondata:
[ { "id": 118748, "price":"", "stocklevel": 100, "instock": false, "pname": "apple tv" }, { "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "hitachi tv"}, { "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "sony tv" }, { "id": 118748, "price":"", "stocklevel": 2345, "instock": true, "pname": "apple tv"}, ... now may have on 100 items in json array, want remove items have duplicate id, sum stock levels , retain order in array row should take place of latest occurrence of id. in above json first instance of object "id": 118748 removed it's stock level value passed / added next instance of object same id, json array so:
[ { "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "hitachi tv"}, { "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "sony tv" }, { "id": 118748, "price":"", "stocklevel": 2445, "instock": true, "pname": "apple tv"}, ... i produced next code remove duplicates, can't sum stock level totals, here code:
function idsareequal(obj1, obj2) { homecoming obj1.id === obj2.id; } function arraycontains(arr, val, equals) { var = arr.length; while (i--) { if (equals(arr[i], val)) { homecoming true; } } homecoming false; } function removedups(arr, equals) { var originalarr = arr.slice(0); var i, k, len, val; arr.length = 0; (i = originalarr.length - 1, len = originalarr.length, k = originalarr.length - 1 ; > 0; --i) { val = originalarr[i]; if (!arraycontains(arr, val, equals)) { arr.push(val); } } } removedups(jsondata, idsareequal); jsondata.reverse(); can please help me solve problem? please note cannot utilize underscore, jquery or other library.
big in advance
you can following, little simpler implementation think. utilize foreach , reduce, es5 should work on modern browsers. can grab polyfil either if you're working older, non-es5 compliant browsers:
var info = [ { "id": 118748, "price":"", "stocklevel": 0, "instock": false, "pname": "apple tv" }, { "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "hitachi tv"}, { "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "sony tv" }, { "id": 118748, "price":"", "stocklevel": 2345, "instock": true, "pname": "apple tv"} ]; function dedup_and_sum(arr, prop) { var seen = {}, order = []; arr.foreach(function(o) { var id = o[prop]; if (id in seen) { // maintain running sum of stocklevel var stocklevel = seen[id].stocklevel + o.stocklevel // maintain newest record's values seen[id] = o; // upid[118805291], stocklevel=432, instock=truedate stocklevel our running total seen[id].stocklevel = stocklevel; // maintain track of ordering, having seen again, force end order.push(order.splice(order.indexof(id), 1)); } else { seen[id] = o; order.push(id); } }); homecoming order.map(function(k) { homecoming seen[k]; }); } // unique records, keeping lastly record of dups // , summing stocklevel go var unique = dedup_and_sum(data, 'id'); unique.foreach(function(o) { console.log("id[%d], stocklevel=%d, instock=%s", o.id, o.stocklevel, o.instock); }); // output => // id[118805291], stocklevel=432, instock=true // id[118801891], stocklevel=0, instock=false // id[118748], stocklevel=2445, instock=true edit updated match requirements discussed in comments.
javascript arrays json
No comments:
Post a Comment