React update object array with values from another array of objects

38 Views Asked by At

In my project I am using a textbox where user can paste a text with a set of questions and answers. I need to get the input split each line in an array and add an object to the setState array with index and the content of the line.

The array of object in setState is

const [question, setQuestion] = React.useState([{quin:0,content:'',examid:''}])

and after the text is pasted in the textbox

function changeText(e){
      let v = e.target.value;
      let ar = v.split('\n');
      let qu = [{quin:0,content:'',examid:''}];
      
      //console.log(ar)
      ar.forEach((element,index) => {
        
          qu.push([...qu,{quin:index,content:element,examid:'ABCD'}])            
        
      });

      setQuestion(qu);
      console.log('q: ' + JSON.stringify(question))
      
  }
0

There are 0 best solutions below