Change only one parameter

If I have a function here:

function printLetters(a="1", b="2", c="3") {
  console.log(a)
  console.log(b)
  console.log(c)
}

And if I want to change c, how do I do it without having to define other parameters like this:

printLetters(1, 2, 4)

I know this is really basic, sorry if I waste your time.

1 Like

@OmegaOrbitals, If you change you code a bit, then you can do it like this where you have to use an object to input the parameters in any order.

function printLetters(values) {
    // get values of a, b, and c, if they are null, use "1", "2", and "3"
    let a = values.a || "1";
    let b = values.b || "2";
    let c = values.c || "3";
    
    console.log(a);
    console.log(b);
    console.log(c);
}

printLetters({ c: "12" });
1 Like

It says cannot read property of undefined values.a

are you using javascript or typescript? because in regular javascript this shouldn’t give an error. Either way, this should work then.

function printLetters(values) {
    // get values of a, b, and c, if they are null, use "1", "2", and "3"
    let keys = Object.keys(values);
    let a = keys.includes("a") ? values.a : "1";
    let b = keys.includes("b") ? values.b : "2";
    let c = keys.includes("c") ? values.c : "3";
    
    console.log(a);
    console.log(b);
    console.log(c);
}

printLetters({ c: "12" });

Regular JavaScript (adding extra text)

I edited the post above to include a solution.

What’s the Object for btw?

That is the base Object class in javascript, you need it to call Object.keys() which gets all the keys in an object. So if you have { a: 4, b: 6, c: "hello"} then it would return ["a", "b", "c"]

1 Like

It’s too complicated, I can’t do this. As you probably know, it’s not only a, b, and c. It’s a LOT of params.

You could copy and paste?

I know, I did it. It didn’t work. I also have some other code after it… so it’s complicated

How about you try this. Inside the defaults are all of your default values, the rest is handled and put into the values object. So to add any new ones, just add a new key to the defaults object.

function printLetters(args) {
    const defaults = { a: "1", b: "2", c: "3" };
    let values = {};
    Object.keys(defaults).forEach(key => {
        values[key] = Object.keys(args).includes(key) ? args[key] : defaults[key];
    });

    console.log(values.a);
    console.log(values.b);
    console.log(values.c);
}

printLetters({ c: "12" });

Now if you just want to input a LOT of parameters, you could do it like this

function myFunc(...args) {
    console.log(args);
}

In this example, args can hold as many parameters as you want.
myFunc(param1, param2, param3, param4, param5...)

1 Like

Thanks, this worked! Though I improvised a little:

let values = {
  a: 1,
  b: 2,
  c: 3
}

if(Object.entries(args).length > 0) {
  Object.keys(args).forEach(key => {
    values[key] = args[key]
  }
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.