How to use _.groupBy js for group object when the properties is in another object - TypeScript












2














I have the follow object



var cars = [
{
'make': 'audi',
'model': 'r8',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'audi',
'model': 'rs5',
'year': '2013',
location: {
'city': 'D',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'mustang',
'year': '2012',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'ford',
'model': 'fusion',
'year': '2015',
location: {
'city': 'A',
'state': 'X',
'country': XX'
}
}, {
'make': 'kia',
'model': 'optima',
'year': '2012',
location: {
'city': 'C',
'state': 'X',
'country': XX'
}
},


];



I would like group cars by city.



So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.



var groups = _.groupBy(cars, 'location.city');


could you please help me?



thanks










share|improve this question





























    2














    I have the follow object



    var cars = [
    {
    'make': 'audi',
    'model': 'r8',
    'year': '2012',
    location: {
    'city': 'A',
    'state': 'X',
    'country': XX'
    }
    }, {
    'make': 'audi',
    'model': 'rs5',
    'year': '2013',
    location: {
    'city': 'D',
    'state': 'X',
    'country': XX'
    }
    }, {
    'make': 'ford',
    'model': 'mustang',
    'year': '2012',
    location: {
    'city': 'A',
    'state': 'X',
    'country': XX'
    }
    }, {
    'make': 'ford',
    'model': 'fusion',
    'year': '2015',
    location: {
    'city': 'A',
    'state': 'X',
    'country': XX'
    }
    }, {
    'make': 'kia',
    'model': 'optima',
    'year': '2012',
    location: {
    'city': 'C',
    'state': 'X',
    'country': XX'
    }
    },


    ];



    I would like group cars by city.



    So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.



    var groups = _.groupBy(cars, 'location.city');


    could you please help me?



    thanks










    share|improve this question



























      2












      2








      2







      I have the follow object



      var cars = [
      {
      'make': 'audi',
      'model': 'r8',
      'year': '2012',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'audi',
      'model': 'rs5',
      'year': '2013',
      location: {
      'city': 'D',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'ford',
      'model': 'mustang',
      'year': '2012',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'ford',
      'model': 'fusion',
      'year': '2015',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'kia',
      'model': 'optima',
      'year': '2012',
      location: {
      'city': 'C',
      'state': 'X',
      'country': XX'
      }
      },


      ];



      I would like group cars by city.



      So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.



      var groups = _.groupBy(cars, 'location.city');


      could you please help me?



      thanks










      share|improve this question















      I have the follow object



      var cars = [
      {
      'make': 'audi',
      'model': 'r8',
      'year': '2012',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'audi',
      'model': 'rs5',
      'year': '2013',
      location: {
      'city': 'D',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'ford',
      'model': 'mustang',
      'year': '2012',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'ford',
      'model': 'fusion',
      'year': '2015',
      location: {
      'city': 'A',
      'state': 'X',
      'country': XX'
      }
      }, {
      'make': 'kia',
      'model': 'optima',
      'year': '2012',
      location: {
      'city': 'C',
      'state': 'X',
      'country': XX'
      }
      },


      ];



      I would like group cars by city.



      So I'm using underscore js, but I don't know how can I access other object in property. I'm trying do it, but not working.



      var groups = _.groupBy(cars, 'location.city');


      could you please help me?



      thanks







      javascript angular typescript underscore.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:13







      Joh

















      asked Nov 21 '18 at 14:28









      JohJoh

      567




      567
























          2 Answers
          2






          active

          oldest

          votes


















          3














          You could use _.property with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.




          _.property(path)



          Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.







          var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
          groups = _.groupBy(cars, _.property(['location', 'city']));

          console.log(groups);

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

          <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








          share|improve this answer





















          • Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
            – Joh
            Nov 21 '18 at 15:51










          • i am sorry about typescript, but i have no idea of it.
            – Nina Scholz
            Nov 21 '18 at 16:05



















          2














          The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.



          It's working for me:



           let groups = _.groupBy(this.cars, car => car.location.city);





          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%2f53414278%2fhow-to-use-groupby-js-for-group-object-when-the-properties-is-in-another-objec%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            You could use _.property with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.




            _.property(path)



            Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.







            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








            share|improve this answer





















            • Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
              – Joh
              Nov 21 '18 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 '18 at 16:05
















            3














            You could use _.property with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.




            _.property(path)



            Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.







            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








            share|improve this answer





















            • Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
              – Joh
              Nov 21 '18 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 '18 at 16:05














            3












            3








            3






            You could use _.property with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.




            _.property(path)



            Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.







            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








            share|improve this answer












            You could use _.property with a path to the property for grouping. This function returns a closure over the path and retuns a function which takes an object for getting the (nested) property.




            _.property(path)



            Returns a function that will return the specified property of any passed-in object. path may be specified as a simple key, or as an array of object keys or array indexes, for deep property fetching.







            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>





            var cars = [{ make: 'audi', model: 'r8', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'audi', model: 'rs5', year: '2013', location: { city: 'A', state: 'X', country: 'XX' } }, { make: 'ford', model: 'mustang', year: '2012', location: { city: 'C', state: 'X', country: 'XX' } }, { make: 'ford', model: 'fusion', year: '2015', location: { city: 'B', state: 'X', country: 'XX' } }, { make: 'kia', model: 'optima', year: '2012', location: { city: 'A', state: 'X', country: 'XX' } }],
            groups = _.groupBy(cars, _.property(['location', 'city']));

            console.log(groups);

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

            <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 14:44









            Nina ScholzNina Scholz

            177k1391156




            177k1391156












            • Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
              – Joh
              Nov 21 '18 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 '18 at 16:05


















            • Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
              – Joh
              Nov 21 '18 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 '18 at 16:05
















            Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
            – Joh
            Nov 21 '18 at 15:51




            Hello Nina, How can I use this code in typescript, because I'm trying put this code in typescript and I get this error: [ts] Argument of type 'string' is not assignable to parameter of type 'string'. [2345]
            – Joh
            Nov 21 '18 at 15:51












            i am sorry about typescript, but i have no idea of it.
            – Nina Scholz
            Nov 21 '18 at 16:05




            i am sorry about typescript, but i have no idea of it.
            – Nina Scholz
            Nov 21 '18 at 16:05













            2














            The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.



            It's working for me:



             let groups = _.groupBy(this.cars, car => car.location.city);





            share|improve this answer


























              2














              The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.



              It's working for me:



               let groups = _.groupBy(this.cars, car => car.location.city);





              share|improve this answer
























                2












                2








                2






                The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.



                It's working for me:



                 let groups = _.groupBy(this.cars, car => car.location.city);





                share|improve this answer












                The Nina's answer is correct for javascript implementation , but I need do it using typescript, so, I fund one solution for me question.



                It's working for me:



                 let groups = _.groupBy(this.cars, car => car.location.city);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 16:56









                JohJoh

                567




                567






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53414278%2fhow-to-use-groupby-js-for-group-object-when-the-properties-is-in-another-objec%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

                    Tonle Sap (See)

                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                    Guatemaltekische Davis-Cup-Mannschaft