Change method in a statement conditionally

Is possible to change a method according to the input without the need to duplicate a function? For example, if the variable intent assumes the value payment, the method applied to the object stripe is paymentIntents. If intent is setup, then the method applied to the object stripe is setupIntent

let intent = req.userIntent

stripe.paymentIntents.update("teste")

What I wouldn’t like to do is:

let intent = req.userIntent

if (intent = "payment") {
     stripe.paymentIntents.update("teste")      
   } else if (intent = "setup") {  
     stripe.setupIntents.update("teste")
 }

Clarifying, the code is way bigger, but the same for both situations, except by the method (paymentIntents or setupIntent), and I don’t want to repeat the code.

Answer

You can use the conditional operator in combination with bracket notation:

const prop = intent === 'payment'
  ? 'paymentIntents'
  : intent === 'setup'
    ? 'setupIntents'
    : null;
stripe[prop]?.update("teste");

or, you may be able to do

stripe[intent + 'intents']?.update("teste");

Note that = is assignment, not comparison. You want === to compare, not =.

If the intent can be only 'payment' or 'setup', it’s easier:

stripe[intent === 'payment' ? 'paymentIntents' : 'setupIntents'].update("teste");

or

stripe[intent + 'intents'].update("teste");