How to group and create new property in javascript object?Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I loop through or enumerate a JavaScript object?How do I include a JavaScript file in another JavaScript file?Convert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Are there any normal animals in Pokemon universe?

Are polynomials with the same roots identical?

Scientist couple raises alien baby

Is it possible for a vehicle to be manufactured without a catalytic converter?

Why can I traceroute to this IP address, but not ping?

What are neighboring ports?

Separate SPI data

How to hide rifle during medieval town entrance inspection?

How can one's career as a reviewer be ended?

Which is the better way to call a method that is only available to one class that implements an interface but not the other one?

Bb13b9 confusion

Printing Pascal’s triangle for n number of rows in Python

Fermat's statement about the ancients: How serious was he?

I have a problematic assistant manager, but I can't fire him

Increase speed altering column on large table to NON NULL

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

New bike, tubeless tire will not inflate

Next date with distinct digits

Can a human be transformed into a Mind Flayer?

If I leave the US through an airport, do I have to return through the same airport?

Electricity free spaceship

Generate basis elements of the Steenrod algebra

Excel division by 0 error when trying to average results of formulas

Has there been a multiethnic Star Trek character?



How to group and create new property in javascript object?


Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I loop through or enumerate a JavaScript object?How do I include a JavaScript file in another JavaScript file?Convert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have an object in javascript:



 "types": [

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 1,
"DocumentName": "doc1"
,

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 2,
"DocumentName": "doc2"
,

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 4,
"DocumentName": "doc4"
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 1,
"DocumentName": "doc1"
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 2,
"DocumentName": "doc2"

]


How can I group by ProcessCode this object to look like this?



"types": [

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,

"DocumentCode": 2,
"DocumentName": "doc2"
,

"DocumentCode": 4,
"DocumentName": "doc4"

]
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,

"DocumentCode": 2,
"DocumentName": "doc2"

]

}
]


I haved try with groupBy of lodash, but It put ProcessCode as key and not like the stucture I want.



 "types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600":  













0















I have an object in javascript:



 "types": [

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 1,
"DocumentName": "doc1"
,

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 2,
"DocumentName": "doc2"
,

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 4,
"DocumentName": "doc4"
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 1,
"DocumentName": "doc1"
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 2,
"DocumentName": "doc2"

]


How can I group by ProcessCode this object to look like this?



"types": [

"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,

"DocumentCode": 2,
"DocumentName": "doc2"
,

"DocumentCode": 4,
"DocumentName": "doc4"

]
,

"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,

"DocumentCode": 2,
"DocumentName": "doc2"

]


]


I haved try with groupBy of lodash, but It put ProcessCode as key and not like the stucture I want.



 "types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600": ...









share
]


I haved try with groupBy of lodash, but It put ProcessCode as key and not like the stucture I want.



 "types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600": ...









share
]


I haved try with groupBy of lodash, but It put ProcessCode as key and not like the stucture I want.



 "types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600": {...






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 19:49









Jon SudJon Sud

799




799






















      1 Answer
      1






      active

      oldest

      votes


















      1














      You can use the function reduce for grouping and the function Object.values to extract the grouped objects.






      let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

      let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

      console.log(result);

      .as-console-wrapper max-height: 100% !important; top: 0; 








      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327886%2fhow-to-group-and-create-new-property-in-javascript-object%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        1 Answer
        1






        active

        oldest

        votes








        1 Answer
        1






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        You can use the function reduce for grouping and the function Object.values to extract the grouped objects.






        let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

        let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

        console.log(result);

        .as-console-wrapper max-height: 100% !important; top: 0; 








        share|improve this answer



























          1














          You can use the function reduce for grouping and the function Object.values to extract the grouped objects.






          let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

          let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

          console.log(result);

          .as-console-wrapper max-height: 100% !important; top: 0; 








          share|improve this answer

























            1












            1








            1







            You can use the function reduce for grouping and the function Object.values to extract the grouped objects.






            let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

            let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

            console.log(result);

            .as-console-wrapper max-height: 100% !important; top: 0; 








            share|improve this answer













            You can use the function reduce for grouping and the function Object.values to extract the grouped objects.






            let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

            let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

            console.log(result);

            .as-console-wrapper max-height: 100% !important; top: 0; 








            let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

            let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

            console.log(result);

            .as-console-wrapper max-height: 100% !important; top: 0; 





            let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];

            let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));

            console.log(result);

            .as-console-wrapper max-height: 100% !important; top: 0; 






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 24 at 19:53









            EleEle

            26.3k52354




            26.3k52354





























                draft saved

                draft discarded
















































                Thanks for contributing an answer to Stack Overflow!


                • Please be sure to answer the question. Provide details and share your research!

                But avoid


                • Asking for help, clarification, or responding to other answers.

                • Making statements based on opinion; back them up with references or personal experience.

                To learn more, see our tips on writing great answers.




                draft saved


                draft discarded














                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327886%2fhow-to-group-and-create-new-property-in-javascript-object%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown





















































                Required, but never shown














                Required, but never shown












                Required, but never shown







                Required, but never shown

































                Required, but never shown














                Required, but never shown












                Required, but never shown







                Required, but never shown







                Popular posts from this blog

                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript