1

Is there a javascript equivalent of this python code? I know you can use ``, but how can you combine it with ... spread operator?

'{}{}{} {}{}{}-{}{}{}{}'.format(*array)

array is list of length 10 of numbers, the result expected is something like

'123 456-7890'
0

1 Answer 1

2

You could replace by shifting the array values.

const
    format = (pattern, [...array]) =>
        pattern.replace(/\{\}/g, Array.prototype.shift.bind(array));

console.log(format('{}{}{} {}{}{}-{}{}{}{}', [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.