1

I am trying to pass an array into a method but having issues with quotes. Here is a case similar to what I am trying to achieve.

const remove = ['blue', 'round', 'tall']

function removeClass(param) {
    foo.classList.remove(param)
}  

removeClass(...remove)

The issue is that the result is foo.classList.remove('blue, round, tall') which won't work. I am trying to achieve this foo.classList.remove('blue', 'round', 'tall')

I have tried using remove.map(el => `'${el}'`).join(',') but then the result is foo.classList.remove("'blue', 'round', 'tall'") which also doesn't work.

2
  • Your removeClass function is designed to only remove one class. You'd need to define a rest parameter or the arguments object to get them all. All depends on how you want to design your function. Commented Mar 21, 2017 at 14:36
  • Or just don't hide behind a function: foo.classList.remove(...remove) Commented Mar 21, 2017 at 14:37

1 Answer 1

3

Try using rest parameters:

const remove = ['blue', 'round', 'tall'];

function removeClass(...param) {
    foo.classList.remove(...param);
}  

removeClass(...remove);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.