Home > OS >  Using strings to improve efficiency of a JavaScript
Using strings to improve efficiency of a JavaScript

Time:01-19

I'm writing a plugin for an opensource utility in JavaScript, where the module I'm writing will interact with an audio interface. I've started coding functionality of some mutes. Instead of writing 32 separate pieces of code I'd like to use a variable to identify the channel that has been changed. For example:

if (address.match('/flx_mute/1')) {
        var mutestatus = message.args[0].value;
        if (mutestatus === 1) {
            this.flexmute1 = "Muted"
            console.log(`Flex Channel 1 is:  ${this.flexmute1}`);
        } else {
            this.flexmute1 = "Unmuted"
            console.log(`Flex Channel 1 is:  ${this.flexmute1}`);
        }
    } 

I know in other languages I could write something like this:

let i = ""
//code here to detmine the channel that needs it's mute status changed...


let flexchannelmute = "flexmute"   i
if (mutestatus === 1) {
        this.[flexchannelmute] = "Muted"
        console.log(`Flex Channel 1 is:  ${this.flexmute1}`);
    } else {
        this.[flexchannelmute] = "Unmuted"
        console.log(`Flex Channel 1 is:  ${this.flexmute1}`);
    }

I did do a bit of google searching to see if I could find the answer to my questions. My apologies if this is a fairly rudimentary topic. The first question is what is this kind of string substitution called? Secondly is what I'm describing possible?

Thanks!

CodePudding user response:

You can create an object (flexChannels) and a ternary statement to store the current status.

let flexChannels = {};

let mutestatus = 1;
let i = 1;
let flexchannelmute = "flexmute"   i

flexChannels[flexchannelmute] = (mutestatus === 1) ? "Muted" : "Unmuted";
console.log(`Flex Channel ${i} is:  ${flexChannels[flexchannelmute]}`);

CodePudding user response:

I could not make out exactly what you are trying to do, but you can try this.

In your top level you appear to be detecting if a message was sent to a particular address. I assume that the part after the first slash denotes that the message is related to the mutes in question and that the number after the second slash is number of the mute the message is for. You can use a regex to match this and get the required info.

The method of accessing a property on an object using a variable as the key is called "bracket notation".

const addressRegex = /\/flx_mute\/(\d )/; // Matches "/flx_mute/" followed by a number
const matchResult = address.match(addressRegex);

// If a match was found...
if (matchResult) {
  const muteStatus = message.args[0].value;
  const muteNumber = matchResult[1]; // This is the number in the address.
  const muteName = "flexmute"   muteNumber;

  if (muteStatus === 1) {
    this[muteName] = "Muted";
    console.log(`Flex Channel ${muteNumber} is:  ${this[muteName]}`);
  } else {
    this[muteName] = "Unmuted";
    console.log(`Flex Channel ${muteNumber} is:  ${this[muteName]}`);
  }
}

You can simplify the code inside the if block by using a ternary expression and a temporary variable:

if (matchResult) {
  const muteStatus = message.args[0].value;
  const muteNumber = matchResult[1];
  const muteName = "flexmute"   muteNumber;

  const muteState = muteStatus === 1 ? "Muted" : "Unmuted";
  this[muteName] = muteState;
  console.log(`Flex Channel ${muteNumber} is:  ${muteState}`);
}
  • Related