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











up vote
2
down vote

favorite












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




























    up vote
    2
    down vote

    favorite












    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


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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 at 16:13

























      asked Nov 21 at 14:28









      Joh

      547




      547
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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 at 15:51










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


















          up vote
          2
          down vote













          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',
            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








            up vote
            3
            down vote



            accepted










            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 at 15:51










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















            up vote
            3
            down vote



            accepted










            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 at 15:51










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













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            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 at 14:44









            Nina Scholz

            170k1383147




            170k1383147












            • 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 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 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 at 15:51










            • i am sorry about typescript, but i have no idea of it.
              – Nina Scholz
              Nov 21 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 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 at 15:51












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




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












            up vote
            2
            down vote













            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

























              up vote
              2
              down vote













              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























                up vote
                2
                down vote










                up vote
                2
                down vote









                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 at 16:56









                Joh

                547




                547






























                     

                    draft saved


                    draft discarded



















































                     


                    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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks