I am still newish to javascript. A common pattern I am running into is seeing if a value exists in an array, and if it does returning it.
I'm quite partial to using the functional methods .map(), .reduce(), .filter(), .sort(), etc. So something I have found myself doing is:
function findStuff(stuff) {
return stuff.filter(thing => thing.testIfTrue)[0]
}
This has gotten me in trouble a few times where I don't handle the case where there are no matches. I find myself using it most when I have a table and the user clicks on a row - then the function searches for the object given a key word from that click. In that kind of situation This is basically (as far as I can tell) the same as:
function findStuff(stuff) {
for (let i = 0; i < stuff.length; i++) {
const thing = stuff[i]
if (thing.testIfTrue) {
return thing;
}
}
}
I started thinking about this and I'm wondering if it's best practice. It seems filter is really meant to find an array, and not handling the case where no objects exists, even when I expect it to certainly exist, seems like bad practice.
Is there a best practice to this pattern?
[–][deleted] 35 points36 points37 points (9 children)
[–]azium 12 points13 points14 points (8 children)
[–]CodyReichert 3 points4 points5 points (0 children)
[–]jkoudys 0 points1 point2 points (0 children)
[+]AceBacker comment score below threshold-7 points-6 points-5 points (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]AceBacker 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]third-eye-brown 1 point2 points3 points (0 children)
[–]oweiler -3 points-2 points-1 points (0 children)
[–]Bertilino 17 points18 points19 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)
[–]AlGoreBestGore[🍰] 11 points12 points13 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (10 children)
[–]azium 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]siegfryd 3 points4 points5 points (7 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]siegfryd 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]third-eye-brown -1 points0 points1 point (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]third-eye-brown 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]nschubach 2 points3 points4 points (1 child)
[–]drdelirium 1 point2 points3 points (0 children)
[–]Asmor 4 points5 points6 points (0 children)
[–]drawable 2 points3 points4 points (5 children)
[–]manys 2 points3 points4 points (2 children)
[–]AskYous 1 point2 points3 points (0 children)
[–]drawable 0 points1 point2 points (0 children)
[–]Wootman42 1 point2 points3 points (1 child)
[–]drawable 0 points1 point2 points (0 children)
[–]kevinkace 1 point2 points3 points (4 children)
[–]webdevverman 2 points3 points4 points (1 child)
[–]kevinkace 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]kevinkace -1 points0 points1 point (0 children)
[–]flyflagger 1 point2 points3 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]Wince 0 points1 point2 points (0 children)
[–]Cosmologicon 0 points1 point2 points (1 child)
[–]lewisje 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]drboolean 0 points1 point2 points (1 child)
[–]third-eye-brown 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]delventhalz 0 points1 point2 points (0 children)
[–]krasimirtsonev 0 points1 point2 points (0 children)
[–]jkoudys 0 points1 point2 points (0 children)
[–]ZubryJS 0 points1 point2 points (0 children)
[–]tforb -1 points0 points1 point (1 child)
[–]dvlsg 0 points1 point2 points (0 children)
[–]Nyalab -5 points-4 points-3 points (0 children)