Ruby hash find base on key value and return an array












-4














I have the following array of hashes:



{
"itens":
[
{"year": "2018", "right": true},
{"year": "2017", "right": true},
{"year": "2019", "right": false}
]
}


I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.



[2018, 2017]









share|improve this question



























    -4














    I have the following array of hashes:



    {
    "itens":
    [
    {"year": "2018", "right": true},
    {"year": "2017", "right": true},
    {"year": "2019", "right": false}
    ]
    }


    I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.



    [2018, 2017]









    share|improve this question

























      -4












      -4








      -4







      I have the following array of hashes:



      {
      "itens":
      [
      {"year": "2018", "right": true},
      {"year": "2017", "right": true},
      {"year": "2019", "right": false}
      ]
      }


      I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.



      [2018, 2017]









      share|improve this question













      I have the following array of hashes:



      {
      "itens":
      [
      {"year": "2018", "right": true},
      {"year": "2017", "right": true},
      {"year": "2019", "right": false}
      ]
      }


      I need to find inside the hashes and return an array containing the year value. Only when the key "right" has value true.



      [2018, 2017]






      ruby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 22:27









      An j

      1




      1
























          4 Answers
          4






          active

          oldest

          votes


















          2














          Given h your data structure



          h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }


          You can add .uniq if you want each year just one time






          share|improve this answer































            1














            Alternatively, looping only once:



            h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
            # => [2018, 2017]





            share|improve this answer





























              0














              How about this:



              items.select { |_| _['right'] }.map { |_| _['year'] }





              share|improve this answer





















              • _ in block parameters is supposed to be used when a parameter is unused inside the block.
                – Marcin Kołodziej
                Nov 22 at 22:33










              • Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                – DjPadz
                Nov 22 at 22:36






              • 1




                Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                – Cary Swoveland
                Nov 25 at 10:07



















              0














              You can try this as well.



              data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
              # => [2018, 2017]


              Or, Alternatively



              data[:itens].map{|a| a[:year].to_i if a[:right]}
              # => [2018, 2017]





              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%2f53438644%2fruby-hash-find-base-on-key-value-and-return-an-array%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                Given h your data structure



                h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }


                You can add .uniq if you want each year just one time






                share|improve this answer




























                  2














                  Given h your data structure



                  h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }


                  You can add .uniq if you want each year just one time






                  share|improve this answer


























                    2












                    2








                    2






                    Given h your data structure



                    h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }


                    You can add .uniq if you want each year just one time






                    share|improve this answer














                    Given h your data structure



                    h[:itens].select { |item| item[:right] }.map { |item| item[:year].to_i }


                    You can add .uniq if you want each year just one time







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 22 at 22:48

























                    answered Nov 22 at 22:29









                    Ursus

                    19.7k31329




                    19.7k31329

























                        1














                        Alternatively, looping only once:



                        h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
                        # => [2018, 2017]





                        share|improve this answer


























                          1














                          Alternatively, looping only once:



                          h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
                          # => [2018, 2017]





                          share|improve this answer
























                            1












                            1








                            1






                            Alternatively, looping only once:



                            h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
                            # => [2018, 2017]





                            share|improve this answer












                            Alternatively, looping only once:



                            h[:itens].each_with_object() { |i, arr| arr << i[:year].to_i if i[:right] }
                            # => [2018, 2017]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 at 22:30









                            Marcin Kołodziej

                            4,191315




                            4,191315























                                0














                                How about this:



                                items.select { |_| _['right'] }.map { |_| _['year'] }





                                share|improve this answer





















                                • _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                  – Marcin Kołodziej
                                  Nov 22 at 22:33










                                • Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                  – DjPadz
                                  Nov 22 at 22:36






                                • 1




                                  Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                  – Cary Swoveland
                                  Nov 25 at 10:07
















                                0














                                How about this:



                                items.select { |_| _['right'] }.map { |_| _['year'] }





                                share|improve this answer





















                                • _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                  – Marcin Kołodziej
                                  Nov 22 at 22:33










                                • Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                  – DjPadz
                                  Nov 22 at 22:36






                                • 1




                                  Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                  – Cary Swoveland
                                  Nov 25 at 10:07














                                0












                                0








                                0






                                How about this:



                                items.select { |_| _['right'] }.map { |_| _['year'] }





                                share|improve this answer












                                How about this:



                                items.select { |_| _['right'] }.map { |_| _['year'] }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 22 at 22:31









                                DjPadz

                                3614




                                3614












                                • _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                  – Marcin Kołodziej
                                  Nov 22 at 22:33










                                • Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                  – DjPadz
                                  Nov 22 at 22:36






                                • 1




                                  Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                  – Cary Swoveland
                                  Nov 25 at 10:07


















                                • _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                  – Marcin Kołodziej
                                  Nov 22 at 22:33










                                • Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                  – DjPadz
                                  Nov 22 at 22:36






                                • 1




                                  Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                  – Cary Swoveland
                                  Nov 25 at 10:07
















                                _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                – Marcin Kołodziej
                                Nov 22 at 22:33




                                _ in block parameters is supposed to be used when a parameter is unused inside the block.
                                – Marcin Kołodziej
                                Nov 22 at 22:33












                                Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                – DjPadz
                                Nov 22 at 22:36




                                Depends on your coding style. Using _ as a parameter when it’s the sole parameter, is considered valid and acceptable style.
                                – DjPadz
                                Nov 22 at 22:36




                                1




                                1




                                Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                – Cary Swoveland
                                Nov 25 at 10:07




                                Yes, _ is a valid local variable and therefore can be used within a block, but the main reason for using an underscore (or any string beginning with an underscore) as a block variable is to tell the reader that that that variable is not used in the block calculation, so no, it's not an acceptable style.
                                – Cary Swoveland
                                Nov 25 at 10:07











                                0














                                You can try this as well.



                                data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
                                # => [2018, 2017]


                                Or, Alternatively



                                data[:itens].map{|a| a[:year].to_i if a[:right]}
                                # => [2018, 2017]





                                share|improve this answer


























                                  0














                                  You can try this as well.



                                  data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
                                  # => [2018, 2017]


                                  Or, Alternatively



                                  data[:itens].map{|a| a[:year].to_i if a[:right]}
                                  # => [2018, 2017]





                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    You can try this as well.



                                    data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
                                    # => [2018, 2017]


                                    Or, Alternatively



                                    data[:itens].map{|a| a[:year].to_i if a[:right]}
                                    # => [2018, 2017]





                                    share|improve this answer












                                    You can try this as well.



                                    data[:itens].keep_if{|a| a[:right]}.map{|b| b[:year].to_i}
                                    # => [2018, 2017]


                                    Or, Alternatively



                                    data[:itens].map{|a| a[:year].to_i if a[:right]}
                                    # => [2018, 2017]






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 23 at 9:49









                                    Wysiati

                                    1044




                                    1044






























                                        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%2f53438644%2fruby-hash-find-base-on-key-value-and-return-an-array%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

                                        Catalogne

                                        Violoncelliste

                                        Héron pourpré