I have an array of objects and I want to create a sequence based on each value. For example take the array:
let starting_data = [
{str: "a"},
{str: "a"},
{str: "b"},
{str: "c"},
{str: "a"},
{str: "c"},
{str: "a"},
{str: "b"}
]
I know I can use a map and reduce to find the unique number of each str value
let uniques = starting_data.map(function(value) {return value.str;}).reduce((acc, val) => {
acc[val] = acc[val] === undefined ? 1 : acc[val] += 1;
return acc;
}, {})
But I'm not sure how to leverage this to eventually get what I want as my desired output:
let desired_output = [
{str: "a", count: 1},
{str: "a", count: 2},
{str: "b", count: 1},
{str: "c", count: 1},
{str: "a", count: 3},
{str: "c", count: 2},
{str: "a", count: 4},
{str: "b", count: 2}
]
Where the counts start over for each unique value, but the as go to 4, and b and c go to 2. I tried messing with arr.sort if I need to do that first but I wasn't sure how to sort based on a value inside an object... Any help appreciated!!