2

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?

10
  • Try using JSON.stringify(data, null, "\t") and paste the resulting object instead of providing a screenshot of DevTools Commented Apr 3, 2021 at 23:44
  • 2
    try const 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), }; }); Commented Apr 3, 2021 at 23:53
  • 1
    take second parameter also as a name. It contains the name of the property Commented Apr 3, 2021 at 23:53
  • 1
    _.map(groups, (element, name) Commented Apr 3, 2021 at 23:53
  • 1
    It’s a static method on Object. Call it with Object.entries(groups).map(([categoryName, categoryValues]) => { ... }). Commented Apr 4, 2021 at 0:40

3 Answers 3

2

You can get the name of the category from the second parameter of the callback function. This callback function is called by the lodash library and when it calls map function 3 arguments (value, key, collection). map

const 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),
  };
});

Sign up to request clarification or add additional context in comments.

2 Comments

This works. Can you please explain why this works though?
How does it recognize as the name of the element?
1

The documentation for map says:

Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments: (value, index|key, collection).

As you can see, the second parameter passed to the iteratee is the original key in the collection. So, just add another parameter to the iteratee and use that:

const categoryValues = _.map(groups, (element, name) => {
    return {
        categoryName: name,
        categoryValue: _.reduce(element,(acc, el) => el.value,0),
    };
});

Comments

0

// this is the simple way.

var arr = {
  iamkeysA : [1,2,3],
  iamkeysB : [4,5,6]
}

var keys_ = Object.keys(arr); // now you get the list of keys name.

console.log( keys_[0] );
console.log( keys_[1] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.