I am looking to develop a custom function that can take an array of objects and specified key(s) for grouping, then create index-based key value pairs based on the specified grouping key(s).
For example:
var iris = [
{"Sepal_Length":1,"Sepal_Width":3.2, "Species":"setosa"},
{"Sepal_Length":1,"Sepal_Width":3.3, "Species":"viridis"},
{"Sepal_Length":1,"Sepal_Width":3.5, "Species":"virsicolor"},
{"Sepal_Length":2,"Sepal_Width":3.7, "Species":"setosa"},
{"Sepal_Length":1,"Sepal_Width":3.2, "Species":"viridis"},
{"Sepal_Length":2,"Sepal_Width":3.8, "Species":"virsicolor"}]
I want to have a function that will group by Species and create a new array with indexes like this:
var iris = [
{"Sepal_Length":1,"Sepal_Width":3.2,"Species":"setosa", "index":1},
{"Sepal_Length":1,"Sepal_Width":3.3,"Species":"viridis", "index":2},
{"Sepal_Length":1,"Sepal_Width":3.5,"Species":"virsicolor", "index":3},
{"Sepal_Length":2,"Sepal_Width":3.7,"Species":"setosa", "index":1},
{"Sepal_Length":1,"Sepal_Width":3.2,"Species":"viridis", "index": 2},
{"Sepal_Length":2,"Sepal_Width":3.8,"Species":"virsicolor", "index": 3}]
I have attempted using map and forEach but as a JavaScript beginner, I am struggling a bit. Any assistance would be greatly appreciated. Thank you!