How to get rid of this unnecessary code while preventing eslint from complaining.

const index = items.findIndex((i) => i.productID == productID); if (index != -1) { const existingItem = items[index]; if (!existingItem) throw 'item not found'; // this will never happen, but without eslint is complaining existingItem.quantity += quantity;// therefore this should be typesafe items[index] = existingItem } preferably I would like to do this, but then eslint is complaining "object is possibly undefined": const index = items.findIndex((i) => i.productID == productID); if (index != -1) { items[index].quantity += quantity; }
No description
4 Replies
Develliot
Develliot•7mo ago
If you are looking through a small array and returning another small array with changes you are probably better off using .map(). It's less memory efficient but has fewer side effects and removes the possibly undefined errors because you are creating as you loop through. Since find() exists I rarely use findIndex()
dan
dan•7mo ago
items[index]!.quanitiy The ! is the non-null assertion operator, its useful for situations like this where you actually 100% of the time know it will exist, though you may get another eslint error depending on your config.
Xanacas
Xanacas•7mo ago
"Since find() exists I rarely use findIndex()" I didn't realize, that find create a mutable item whose changes are also reflected in the array. amazing 🙂
Develliot
Develliot•7mo ago
In React land we often go out of our way to avoid doing things with side effects and create new arrays rather than mutate the thing we are iterating over.
Want results from more Discord servers?
Add your server
More Posts
trpc mutations for fetching dataRecently was told that using mutations to fetch list data is bad. I can agree with this on a high leMaking all my components 'use client' while maintaining protectedProceduresHi. I'm using the full T3 stack with prisma, next auth, and nextjs with the app router. I want to siServer Component Data RevalidationHi all, searched and did not find a discussion on this yet but new here so apologies if I overlookedPrettier hangs when using with tailwind pluginFirst I've noticed it in VSCode, than tried via CLI - the same error. Without plugin everything worknext dont build in github ciHey guys I started building an app with the t3 stack a few days ago, and now I wanted to write a Gitcreate t3 app @ latest mutation result not mappableIve made sample t3 app with the latest build just to create a trpc router that helps me reach an extGraphQl and NextJS 13+ for multiple, concurrent/parallel DB queries?I know this could be a problem with architecture/design, but how would you most efficiently query (rLinks within one another (nextjs)Hey, having a problem with having link buttons within each other. When I try to give the bottom buttServer component rendered by client component as children prop throwing errori'm trying to fetch some data from a server component (`StatusImages`) that is being rendered as theEnhancing Performance with Pre-fetching Paginated Queries in tRPCHello, I'm currently focused on boosting the performance of a website that incorporates tRPC. Our s