[Solved] how to convert user input of string to object in javascript


As per your question, assuming the user enters text strictly in the format code: x code: y code: z available to you as a string, you may do something like:

`code: 2a31sdd23
code: asdw12ds3
code: 213sad123`
.replace(/[\r\n]*/g,'')
.replace(/code:\s+/g,'')
.replace(/[\s\t]+/g,' ')
.split(' ')
.reduce((p,c) => {p.push({code: c}); return p}, [])

// Output
// [{code: "2a31sdd23"}, {code: "asdw12ds3"}, {code: "213sad123"}]

4

solved how to convert user input of string to object in javascript