I have some Entries, those entries have a Category assigned to them, and each entry has a numeric Value;
{
category: "cat1",
value: -100
}
This is my entry ^. I run a Loadash GroupBy to group all the categories.
const groups = _.groupBy(dataSource, (entry) => entry.category);
This snippet here returns like this:
{
"cat1": [
{
category: "cat1",
value: -100
},
{
category: "cat1",
value: +10
},
],
"cat2": [
{
category: "cat2",
value: -100
},
{
category: "cat2",
value: +40
},
//and so on...
}
The keys are the Category names. The objects are the related entries.
I need to run a consecutive Map with an embedded Reduce to reduce the arrays to an integer through the sum of each entry's value.
const categoryValues = _.map(groups, (element) => {
return {
categoryName: ???????,
//*I DONT KNOW WHAT GOES HERE ^,
//I NEED THE NAME OF THE CATEGORY TO BE HERE*
categoryValue: _.reduce(element,(acc, el) => el.value,0),
};
});
Thats because my graph api needs his dataset array to be formed by objects like this one:
{
"categoryName": "cat1", //*THIS IS MISSING*
"categoryValue": 999
}
How can I access the key name of each array? I need to know how the category is named, so that the graph will display its name.
What needs to be written where I put my question marks?
JSON.stringify(data, null, "\t")and paste the resulting object instead of providing a screenshot of DevToolsconst categoryValues = _.map(groups, (element, name) => { return { categoryName: name, //I DONT KNOW WHAT GOES HERE, I NEED THE NAME OF THE CATEGORY TO BE HERE* categoryValue: _.reduce(element, (acc, el) => el.value, 0), }; });name. It contains the name of the property_.map(groups, (element, name)Object.entries(groups).map(([categoryName, categoryValues]) => { ... }).