I implemented the idea of OTP input in react and the result was an object as follows :
{input1: '8', input2: '8', input3: '8', input4: '8'}
How to convert this object into a continuous number as follows : 8888
thank you
I implemented the idea of OTP input in react and the result was an object as follows :
{input1: '8', input2: '8', input3: '8', input4: '8'}
How to convert this object into a continuous number as follows : 8888
thank you
5
Answers
First of all, It is better to make OTP input field as a single field. Seems you are using 4 separate input fields.
Anyway, the solution for your problem is, that you need to get Object values and
join
the object values as one string, Then convert it to aNumber
.If you are not certain of the order of the keys then you could use
Since the entries will be alphabetically sortable we can do this if sort is needed
Don’t do this, but it is really tempting to use
with
here (worth the mandatory downvotes 😉 )This question lacks precision…
but assuming that
input
followed by numbers between 1 and 4,0
to9
Using an index to dynamically order the object keys correctly: