React JS: Refactoring Redux Selectors





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







0















I am using several selectors to get the boundaries on which to set my Google Maps. They basically return the lowest and highest lat/lng points from the data. The code works but I feel this is really messy and can be re-factored, but i'm not too sure how.



Here is the code:



const getMinLat = data =>
data.reduce((prev, current) => {
return prev.coords[0] < current.coords[0] ? prev : current
})

const getMaxLat = data =>
data.reduce((prev, current) => {
return prev.coords[0] > current.coords[0] ? prev : current
})

const getMinLng = data =>
data.reduce((prev, current) => {
return prev.coords[1] < current.coords[1] ? prev : current
})

const getMaxLng = data =>
data.reduce((prev, current) => {
return prev.coords[1] > current.coords[1] ? prev : current
})

const getBounds = data => [
{
lat: getMinLat(data).coords[0],
lng: getMinLng(data).coords[1],
},
{
lat: getMaxLat(data).coords[0],
lng: getMaxLng(data).coords[1],
},
]









share|improve this question





























    0















    I am using several selectors to get the boundaries on which to set my Google Maps. They basically return the lowest and highest lat/lng points from the data. The code works but I feel this is really messy and can be re-factored, but i'm not too sure how.



    Here is the code:



    const getMinLat = data =>
    data.reduce((prev, current) => {
    return prev.coords[0] < current.coords[0] ? prev : current
    })

    const getMaxLat = data =>
    data.reduce((prev, current) => {
    return prev.coords[0] > current.coords[0] ? prev : current
    })

    const getMinLng = data =>
    data.reduce((prev, current) => {
    return prev.coords[1] < current.coords[1] ? prev : current
    })

    const getMaxLng = data =>
    data.reduce((prev, current) => {
    return prev.coords[1] > current.coords[1] ? prev : current
    })

    const getBounds = data => [
    {
    lat: getMinLat(data).coords[0],
    lng: getMinLng(data).coords[1],
    },
    {
    lat: getMaxLat(data).coords[0],
    lng: getMaxLng(data).coords[1],
    },
    ]









    share|improve this question

























      0












      0








      0








      I am using several selectors to get the boundaries on which to set my Google Maps. They basically return the lowest and highest lat/lng points from the data. The code works but I feel this is really messy and can be re-factored, but i'm not too sure how.



      Here is the code:



      const getMinLat = data =>
      data.reduce((prev, current) => {
      return prev.coords[0] < current.coords[0] ? prev : current
      })

      const getMaxLat = data =>
      data.reduce((prev, current) => {
      return prev.coords[0] > current.coords[0] ? prev : current
      })

      const getMinLng = data =>
      data.reduce((prev, current) => {
      return prev.coords[1] < current.coords[1] ? prev : current
      })

      const getMaxLng = data =>
      data.reduce((prev, current) => {
      return prev.coords[1] > current.coords[1] ? prev : current
      })

      const getBounds = data => [
      {
      lat: getMinLat(data).coords[0],
      lng: getMinLng(data).coords[1],
      },
      {
      lat: getMaxLat(data).coords[0],
      lng: getMaxLng(data).coords[1],
      },
      ]









      share|improve this question














      I am using several selectors to get the boundaries on which to set my Google Maps. They basically return the lowest and highest lat/lng points from the data. The code works but I feel this is really messy and can be re-factored, but i'm not too sure how.



      Here is the code:



      const getMinLat = data =>
      data.reduce((prev, current) => {
      return prev.coords[0] < current.coords[0] ? prev : current
      })

      const getMaxLat = data =>
      data.reduce((prev, current) => {
      return prev.coords[0] > current.coords[0] ? prev : current
      })

      const getMinLng = data =>
      data.reduce((prev, current) => {
      return prev.coords[1] < current.coords[1] ? prev : current
      })

      const getMaxLng = data =>
      data.reduce((prev, current) => {
      return prev.coords[1] > current.coords[1] ? prev : current
      })

      const getBounds = data => [
      {
      lat: getMinLat(data).coords[0],
      lng: getMinLng(data).coords[1],
      },
      {
      lat: getMaxLat(data).coords[0],
      lng: getMaxLng(data).coords[1],
      },
      ]






      javascript reactjs redux react-redux selector






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 13:30









      ShhShh

      104115




      104115
























          3 Answers
          3






          active

          oldest

          votes


















          1














          just traverse list once:



          const getBounds = data => 
          data.reduce(
          ([
          {lat: minLat, lng: minLng},
          {lat: maxLat, lng: maxLng}
          ],
          {coords: [lat, lng]}
          ) =>
          [
          {
          lat: Math.min(minLat, lat),
          lng: Math.min(minLng, lng)
          },
          {
          lat: Math.max(maxLat, lat),
          lng: Math.max(maxLng, lng)
          }
          ],
          [{lat: Infinity, lng: Infinity}, {lat: -Infinity, lng: -Infinity}]
          )


          I agree in advance that readability is not the advantage here. But it goes through list just once.



          Also after getting used to destructuring syntax it's quite clear that Math.max and maxLng/maxLat goes together to decrease possibility of using wrong variable



          [UPD] and there is no need to use Infinity/-Infinity as start values(I used them to highlight idea behind). for longitude/latitude we may use 180/-180 as most extreme values






          share|improve this answer

































            1














            Maybe this



            const getMin = (data, index) =>
            data.reduce((prev, current) => (prev.coords[index] < current.coords[index] ? prev : current))
            .coords[index];

            const getMax = (data, index) =>
            data.reduce((prev, current) => (prev.coords[index] > current.coords[index] ? prev : current))
            .coords[index];

            const getBounds = data => [
            {
            lat: getMin(data, 0),
            lng: getMin(data, 1)
            },
            {
            lat: getMax(data, 0),
            lng: getMax(data, 1)
            }
            ];





            share|improve this answer































              1














              You can make it more concise by using Array.map():



              const getCoordsArray = (data, index) =>
              data.map(o => o.coords[index]);

              const getMin = (data, index) =>
              Math.min(...getCoordsArray(data, index));

              const getMax = (data, index) =>
              Math.max(...getCoordsArray(data, index));

              const getBounds = data => [getMin, getMax]
              .map(m => ({
              lat: m(data, 0),
              lng: m(data, 1),
              }));





              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%2f53447632%2freact-js-refactoring-redux-selectors%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                just traverse list once:



                const getBounds = data => 
                data.reduce(
                ([
                {lat: minLat, lng: minLng},
                {lat: maxLat, lng: maxLng}
                ],
                {coords: [lat, lng]}
                ) =>
                [
                {
                lat: Math.min(minLat, lat),
                lng: Math.min(minLng, lng)
                },
                {
                lat: Math.max(maxLat, lat),
                lng: Math.max(maxLng, lng)
                }
                ],
                [{lat: Infinity, lng: Infinity}, {lat: -Infinity, lng: -Infinity}]
                )


                I agree in advance that readability is not the advantage here. But it goes through list just once.



                Also after getting used to destructuring syntax it's quite clear that Math.max and maxLng/maxLat goes together to decrease possibility of using wrong variable



                [UPD] and there is no need to use Infinity/-Infinity as start values(I used them to highlight idea behind). for longitude/latitude we may use 180/-180 as most extreme values






                share|improve this answer






























                  1














                  just traverse list once:



                  const getBounds = data => 
                  data.reduce(
                  ([
                  {lat: minLat, lng: minLng},
                  {lat: maxLat, lng: maxLng}
                  ],
                  {coords: [lat, lng]}
                  ) =>
                  [
                  {
                  lat: Math.min(minLat, lat),
                  lng: Math.min(minLng, lng)
                  },
                  {
                  lat: Math.max(maxLat, lat),
                  lng: Math.max(maxLng, lng)
                  }
                  ],
                  [{lat: Infinity, lng: Infinity}, {lat: -Infinity, lng: -Infinity}]
                  )


                  I agree in advance that readability is not the advantage here. But it goes through list just once.



                  Also after getting used to destructuring syntax it's quite clear that Math.max and maxLng/maxLat goes together to decrease possibility of using wrong variable



                  [UPD] and there is no need to use Infinity/-Infinity as start values(I used them to highlight idea behind). for longitude/latitude we may use 180/-180 as most extreme values






                  share|improve this answer




























                    1












                    1








                    1







                    just traverse list once:



                    const getBounds = data => 
                    data.reduce(
                    ([
                    {lat: minLat, lng: minLng},
                    {lat: maxLat, lng: maxLng}
                    ],
                    {coords: [lat, lng]}
                    ) =>
                    [
                    {
                    lat: Math.min(minLat, lat),
                    lng: Math.min(minLng, lng)
                    },
                    {
                    lat: Math.max(maxLat, lat),
                    lng: Math.max(maxLng, lng)
                    }
                    ],
                    [{lat: Infinity, lng: Infinity}, {lat: -Infinity, lng: -Infinity}]
                    )


                    I agree in advance that readability is not the advantage here. But it goes through list just once.



                    Also after getting used to destructuring syntax it's quite clear that Math.max and maxLng/maxLat goes together to decrease possibility of using wrong variable



                    [UPD] and there is no need to use Infinity/-Infinity as start values(I used them to highlight idea behind). for longitude/latitude we may use 180/-180 as most extreme values






                    share|improve this answer















                    just traverse list once:



                    const getBounds = data => 
                    data.reduce(
                    ([
                    {lat: minLat, lng: minLng},
                    {lat: maxLat, lng: maxLng}
                    ],
                    {coords: [lat, lng]}
                    ) =>
                    [
                    {
                    lat: Math.min(minLat, lat),
                    lng: Math.min(minLng, lng)
                    },
                    {
                    lat: Math.max(maxLat, lat),
                    lng: Math.max(maxLng, lng)
                    }
                    ],
                    [{lat: Infinity, lng: Infinity}, {lat: -Infinity, lng: -Infinity}]
                    )


                    I agree in advance that readability is not the advantage here. But it goes through list just once.



                    Also after getting used to destructuring syntax it's quite clear that Math.max and maxLng/maxLat goes together to decrease possibility of using wrong variable



                    [UPD] and there is no need to use Infinity/-Infinity as start values(I used them to highlight idea behind). for longitude/latitude we may use 180/-180 as most extreme values







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 23 '18 at 14:14

























                    answered Nov 23 '18 at 14:01









                    skyboyerskyboyer

                    4,28811333




                    4,28811333

























                        1














                        Maybe this



                        const getMin = (data, index) =>
                        data.reduce((prev, current) => (prev.coords[index] < current.coords[index] ? prev : current))
                        .coords[index];

                        const getMax = (data, index) =>
                        data.reduce((prev, current) => (prev.coords[index] > current.coords[index] ? prev : current))
                        .coords[index];

                        const getBounds = data => [
                        {
                        lat: getMin(data, 0),
                        lng: getMin(data, 1)
                        },
                        {
                        lat: getMax(data, 0),
                        lng: getMax(data, 1)
                        }
                        ];





                        share|improve this answer




























                          1














                          Maybe this



                          const getMin = (data, index) =>
                          data.reduce((prev, current) => (prev.coords[index] < current.coords[index] ? prev : current))
                          .coords[index];

                          const getMax = (data, index) =>
                          data.reduce((prev, current) => (prev.coords[index] > current.coords[index] ? prev : current))
                          .coords[index];

                          const getBounds = data => [
                          {
                          lat: getMin(data, 0),
                          lng: getMin(data, 1)
                          },
                          {
                          lat: getMax(data, 0),
                          lng: getMax(data, 1)
                          }
                          ];





                          share|improve this answer


























                            1












                            1








                            1







                            Maybe this



                            const getMin = (data, index) =>
                            data.reduce((prev, current) => (prev.coords[index] < current.coords[index] ? prev : current))
                            .coords[index];

                            const getMax = (data, index) =>
                            data.reduce((prev, current) => (prev.coords[index] > current.coords[index] ? prev : current))
                            .coords[index];

                            const getBounds = data => [
                            {
                            lat: getMin(data, 0),
                            lng: getMin(data, 1)
                            },
                            {
                            lat: getMax(data, 0),
                            lng: getMax(data, 1)
                            }
                            ];





                            share|improve this answer













                            Maybe this



                            const getMin = (data, index) =>
                            data.reduce((prev, current) => (prev.coords[index] < current.coords[index] ? prev : current))
                            .coords[index];

                            const getMax = (data, index) =>
                            data.reduce((prev, current) => (prev.coords[index] > current.coords[index] ? prev : current))
                            .coords[index];

                            const getBounds = data => [
                            {
                            lat: getMin(data, 0),
                            lng: getMin(data, 1)
                            },
                            {
                            lat: getMax(data, 0),
                            lng: getMax(data, 1)
                            }
                            ];






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 13:38









                            Safi NettahSafi Nettah

                            442511




                            442511























                                1














                                You can make it more concise by using Array.map():



                                const getCoordsArray = (data, index) =>
                                data.map(o => o.coords[index]);

                                const getMin = (data, index) =>
                                Math.min(...getCoordsArray(data, index));

                                const getMax = (data, index) =>
                                Math.max(...getCoordsArray(data, index));

                                const getBounds = data => [getMin, getMax]
                                .map(m => ({
                                lat: m(data, 0),
                                lng: m(data, 1),
                                }));





                                share|improve this answer






























                                  1














                                  You can make it more concise by using Array.map():



                                  const getCoordsArray = (data, index) =>
                                  data.map(o => o.coords[index]);

                                  const getMin = (data, index) =>
                                  Math.min(...getCoordsArray(data, index));

                                  const getMax = (data, index) =>
                                  Math.max(...getCoordsArray(data, index));

                                  const getBounds = data => [getMin, getMax]
                                  .map(m => ({
                                  lat: m(data, 0),
                                  lng: m(data, 1),
                                  }));





                                  share|improve this answer




























                                    1












                                    1








                                    1







                                    You can make it more concise by using Array.map():



                                    const getCoordsArray = (data, index) =>
                                    data.map(o => o.coords[index]);

                                    const getMin = (data, index) =>
                                    Math.min(...getCoordsArray(data, index));

                                    const getMax = (data, index) =>
                                    Math.max(...getCoordsArray(data, index));

                                    const getBounds = data => [getMin, getMax]
                                    .map(m => ({
                                    lat: m(data, 0),
                                    lng: m(data, 1),
                                    }));





                                    share|improve this answer















                                    You can make it more concise by using Array.map():



                                    const getCoordsArray = (data, index) =>
                                    data.map(o => o.coords[index]);

                                    const getMin = (data, index) =>
                                    Math.min(...getCoordsArray(data, index));

                                    const getMax = (data, index) =>
                                    Math.max(...getCoordsArray(data, index));

                                    const getBounds = data => [getMin, getMax]
                                    .map(m => ({
                                    lat: m(data, 0),
                                    lng: m(data, 1),
                                    }));






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 23 '18 at 22:04

























                                    answered Nov 23 '18 at 13:56









                                    Ori DroriOri Drori

                                    82.3k148998




                                    82.3k148998






























                                        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%2f53447632%2freact-js-refactoring-redux-selectors%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

                                        "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                        Alcedinidae

                                        Origin of the phrase “under your belt”?