मेरे पास विवरण डेटा है जैसे:

"<question> This is question 1? </question> 
<answer> This is answer 1 </answer>
<question> this is question 2? </question>
<answer> This is answer </answer>"

वांछित आउटपुट है:

 desc= {
   [question: "This is question 1?", answer: "This is answer 1"], 
   [question: "this is question 2?", answer: "This is answer 2] 
 }
0
kalyani_jamunkar 10 पद 2018, 09:34

1 उत्तर

सबसे बढ़िया उत्तर

इसकी जांच करें:

const desc = "<question> This is question 1? </question><answer> This is answer 1 
</answer><question> this is question 2? </question><answer> This is answer </answer>";

var questions = desc.match(/<question>(.*?)<\/question>/g).map(function(val){
return val.replace(/<\/?question>/g,'');
});

var answers = desc.match(/<answer>(.*?)<\/answer>/g).map(function(val){
return val.replace(/<\/?answer>/g,'');
});

var arrayList = questions.map((item,index) => {
return {question:item, answer: answers[index]}
})

console.log(JSON.stringify(arrayList));

कार्य समाधान:

https://codepen.io/anon/pen/NeWEdz

1
Anil Kumar 10 पद 2018, 09:50