Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Last active April 1, 2022 15:40
Show Gist options
  • Save Andaeiii/51cc65999eda675edaf68d6baa4cefa1 to your computer and use it in GitHub Desktop.
Save Andaeiii/51cc65999eda675edaf68d6baa4cefa1 to your computer and use it in GitHub Desktop.
compute the json object of each item in the shopping cart using the Ids
const computeStats = () => {
if (cart.items.length !== 0) {
var ids = cart.items.map(item => item.id);
let isUnique = (value, index, self) => self.indexOf(value) === index;
let distinct = ids.filter(isUnique);
distinct.forEach(id => {
stats.push({
"id": id,
'count': cart.items.filter(o => o.id === id).length,
'clicked': (id === curId) ? 'true' : 'false'
});
});
}
return stats.sort((a, b) => b.count - a.count);
}
@Andaeiii
Copy link
Author

Andaeiii commented May 10, 2021

I modelled a shopping application, and in building the shopping cart,

  1. i needed to keep track of the count of each item being selected (frequency)
  2. i had to also ensure the item with the highest count was always always first at the top
  3. i wanted the last chosen item to be tracked (boldened) in the entire
  4. it should return a JSON object to render the component with.

i also used it to calculate the total of the items.. the image below

Drag Racing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment