0

I have the same function repeating over and over again. I'd like to combine it into one so that I can call it without having to write long code.

totalV
    .filter((nan) => nan)
    .reduce((a, b) => a + b, 0)
    .toFixed(2)

Basically, I want to save it into a const or something.. and then call it whenever needed I tried to do something like this:

const Vol =
    .filter((nan) => nan)
    .reduce((a, b) => a + b, 0)
    .toFixed(2)

But unfortunately I wasn't able to do so, because it's asking for "expression" or array to do this funciton on ( something before EXPRESSION.filter.reduce )

EDIT: MORE INFO

{
              totalV.filter((nan) => nan)
              .reduce((a, b) => a + b, 0)
              .toFixed(2) == 0.0
              ? 0
              : 
                  totalV.filter((nan) => nan)
                  .reduce((a, b) => a + b, 0)
                  .toFixed(2)}

So I have this conditional render that I'm writing in a few spots.. however totalV can change, but the rest of the code is the same.. so I thought to store the code that keeps repeating into a variable or something like

const repeatableCode = 
.filter((nan) => nan)
.reduce((a, b) => a + b, 0)
.toFixed(2)

but it refuses to work without an array/object being provided ahead of time

5
  • 1
    Can you give more context around how you invoke this logic, and what the value of totalV is/could be. It seems you could just extract the logic to a function and call that when needed. Commented Nov 9, 2022 at 11:38
  • 1
    const doSomething = array => array.filter().reduce().toFixed(); Commented Nov 9, 2022 at 11:40
  • 1
    const App= (a, b) => {} and ab can be variable Commented Nov 9, 2022 at 11:40
  • 1
    Vol = totalV => totalV.filter((nan) => nan).reduce((a, b) => a + b, 0).toFixed(2) or even older style with function(totalV) { return totalV.filter/*... */ } Just make a regular function that takes a regular parameter and use it regularly. No need to try and find some weird way of preserving the method chain to append elsewhere Commented Nov 9, 2022 at 11:41
  • 1
    With the more info: why not just store the result of that expression in a variable? result = totalV.filter/*... */ ; result ? 0 : result Or you could even just use a fallback with totalV.filter/*... */ ?? 0 or totalV.filter/*... */ || 0 (depending on what you need When should I use ?? (nullish coalescing) vs || (logical OR)?) Commented Nov 9, 2022 at 11:45

2 Answers 2

0

Why just not

const complexArrayFunc = (array) => {
  if (!array) return []
  return array.filter((nan) => nan)
    .reduce((a, b) => a + b, 0)
    .toFixed(2)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use lambdas (anonymous functions / arrow function expressions):

for example:

const repeatableCode = (totalV) => {
    return totalV
        .filter((nan) => nan)
        .reduce((a, b) => a + b, 0)
        .toFixed(2)
}

Learn more about it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.