1

I am wanting to concat an indeterminate amount of string arguments with a specified delimiter, so I Google'd the issue and came across a page on Mozilla's website instructing on how to do just that using the arguments object.

function myConcat(separator) {
  var args = Array.prototype.slice.call(arguments, 1);
  return args.every(x => x === '') ? '' : args.join(separator);
}

When I tried this in a normal JS compiler such as repl.it, it worked perfectly! I couldn't be happier to have it proven, tried and tested.

However, when plugging the code into my Angular 6 app, I get the usual error stating that when calling the function, I am passing in too many arguments and that it only expects the one.

Is there anything I can do to get this working in Angular 6?

3
  • Have you tried using rest parameters instead of relying on the arguments object? Commented Oct 19, 2018 at 10:52
  • If I knew what that meant, I probably would have :-P Commented Oct 19, 2018 at 10:53
  • something like this function myConcat(separator, ...strings) { return strings.every(x => x === '') ? '' : strings.join(separator); } Commented Oct 19, 2018 at 10:55

1 Answer 1

2

It might work if you use rest parameters instead of the arguments object:

function myConcat(separator, ...strings) {
 return strings.every(x => x === '') ? '' : strings.join(separator);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Aha! I'd been looking for how I could pass an arg along with a long arg! I must have had it the wrong way around when I tried (...args, sep).
@physicsboy Yes, rest parameters have to be the last argument to a function.
I shall bear that in mind for future! Thanks for the help :-)

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.