I've been trying for days now to come up with a solution to my problem and I just can't, imagine I have the following JSON array (we shall call this 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 I may have over 100 items in my JSON array, I want to remove items which have a duplicate id, but sum the stock levels and retain the order in the array so a row should take the place of the latest occurrence of that id. In the above JSON the first instance of the object with "id": 118748 is removed but it's stock level value passed / added the next instance of an object with the same id, so the JSON Array would look like 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 the following code to remove the duplicates, but I can't sum the stock level totals, here is my code:
function idsAreEqual(obj1, obj2) {
return obj1.id === obj2.id;
}
function arrayContains(arr, val, equals) {
var i = arr.length;
while (i--) {
if (equals(arr[i], val)) {
return true;
}
}
return false;
}
function removeDups(arr, equals) {
var originalArr = arr.slice(0);
var i, k, len, val;
arr.length = 0;
for (i = originalArr.length - 1, len = originalArr.length, k = originalArr.length - 1 ; i > 0; --i) {
val = originalArr[i];
if (!arrayContains(arr, val, equals)) {
arr.push(val);
}
}
}
removeDups(jsonData, idsAreEqual);
jsonData.reverse();
Can someone please help me solve this problem? Please note that I cannot use Underscore, jQuery or any other library.
Big thanks in advance