Skip to content

Instantly share code, notes, and snippets.

@mystikraz
Created February 1, 2022 10:36
Show Gist options
  • Select an option

  • Save mystikraz/1f62437a5264fc9858319cf7277bfa71 to your computer and use it in GitHub Desktop.

Select an option

Save mystikraz/1f62437a5264fc9858319cf7277bfa71 to your computer and use it in GitHub Desktop.
group by in Javascript
//arranging groupBy deviceId and Qty
var result = [];
cartItems.reduce(function (res, value) {
if (!res[value.deviceId]) {
res[value.deviceId] = { deviceId: value.deviceId, qty: 0 };
result.push(res[value.deviceId])
}
res[value.deviceId].qty += value.qty;
return res;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment