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;
}
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
add a comment |
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
add a comment |
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
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
javascript reactjs redux react-redux selector
asked Nov 23 '18 at 13:30
ShhShh
104115
104115
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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)
}
];
add a comment |
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),
}));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Nov 23 '18 at 14:14
answered Nov 23 '18 at 14:01
skyboyerskyboyer
4,28811333
4,28811333
add a comment |
add a comment |
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)
}
];
add a comment |
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)
}
];
add a comment |
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)
}
];
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)
}
];
answered Nov 23 '18 at 13:38
Safi NettahSafi Nettah
442511
442511
add a comment |
add a comment |
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),
}));
add a comment |
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),
}));
add a comment |
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),
}));
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),
}));
edited Nov 23 '18 at 22:04
answered Nov 23 '18 at 13:56
Ori DroriOri Drori
82.3k148998
82.3k148998
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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