1

Is there a pretty way of building a string of something like (?,?,?) for the number of items in an array

I've tried

values.map(() => '?,')
values.reduce((a,b) => {a + '?,'},'')

but both are not working

4 Answers 4

2

You could map question marks for each element and join the array in an template literal.

var array = [1, 2, 3],
    string = `(${array.map(_ => '?').join()})`;
    
console.log(string);

Sign up to request clarification or add additional context in comments.

Comments

1
  "(" + values.map(el => "?").join() + ")"

You could just join them.

Comments

0

You need to return from map and reduce functions.

var valuse = [1,2,3]
var newValues = values.reduce((a,b) => a === '' ? '?' : a + ',?','');
console.log(newValues);

Comments

0

You could simply use the arrays forEach() extension, like this:

var string = "";
[1,2,3,4,5,6,7,8,9].forEach((elem, index) => string += "?");

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.