简体   繁体   中英

Get key name of a javascript object in an array of objects

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?

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

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

 // 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] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM