大佬教程收集整理的这篇文章主要介绍了【angular】ts截取字符串、json,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
因为我要做的界面和组长的界面差不多所以我直接拷贝的代码,本来是好好的、安安静静地在那里展示着,但是不知道怎么回事,全乱了,所以调样式吧,后台传给我的是一张试卷的json?,接到之后转格式
改之后的代码,这样展示的是一行,将 什么的都去掉了:
ngOnInit() {
if (this.question.stuscore == "" || this.question.stuscore == null) {
this.question.stuscore = "0"
}
if (this.question.studentAnswer.includes("<br>")) {
this.changeWordForShow(this.question.studentAnswer);
}
}
changeWordForShow(student@R_636_5696@ata: String) {
let arg: String[] = this.question.studentAnswer.split("<br>");
let textForAnswer: String = "";
for (var i = 0; i < arg.length; i++) {
textForAnswer = textForAnswer + arg[i].toString();
}
this.question.studentAnswer=textForAnswer;
let lastThingForAnswer: String = '';
if (textForAnswer.includes(" ")) {
let dataMerge:any = this.question.studentAnswer.split(" ");
dataMerge.forEach(eleIndex => {
lastThingForAnswer = lastThingForAnswer + eleIndex.toString();
});
this.question.studentAnswer = lastThingForAnswer;
} else {
this.question.studentAnswer = textForAnswer;
}
}
之前组长的代码,这样是多行展示的:对用户来说更好看一点,但是不起作用、诶
ngOnInit() {
this.questionAnswer = this.turnAnswer(this.question.answer);
this.studentAnswer = this.turnAnswer(this.question.studentAnswer);
}
具体的操作
//多个答案间 会有 | 这样就不是 json 了,转json会报错 所以先分割成数组在一次转格式
turnAnswer(answer) {
if (answer != null && answer.length > 0) {
if (answer.includes("|")) {
let echoBusinessList: Array<String> = answer.split("|");
// private questionSubList = new Array<QuestionSubEntity>(); //页面上显示的答案
// for (let i = 0; i < echoBusinessList.length; i++) {
// this.questionSubList.push({ optionOrder: i + 1,optionsContent: '',questionMainId: '' });
// }
let dataMsg: any="";
echoBusinessList.forEach(x => {
dataMsg = dataMsg + this.processAnswer(JSON.parse(X));
});
return dataMsg;
} else {
return this.processAnswer(JSON.parse(answer));
}
}
}
/* 处理答案 */
private processAnswer(businessList: BussinessModel[]) {
let answerForShow: String = "";
businessList.forEach((element,indeX) => { answerForShow += "业务" + (index + 1) + " : " + element.explain + "<br>"; answerForShow += this.joinAnswer(element.borrowList); answerForShow += this.joinAnswer(element.loanList); answerForShow += "<br>" }) return answerForShow; //这个是因为上面的不起作用, 就只有显出来了,我这里不起作用,我们组长那里貌似好好的,悲伤~~ // let answerForShow: String = ""; // businessList.forEach((element,indeX) => { // answerForShow += "业务" + (index + 1) + " :" + element.explain + " "; // answerForShow += this.joinAnswer(element.borrowList); // answerForShow += this.joinAnswer(element.loanList); // answerForShow += " ;" // }) // return answerForShow; }
连接答案
/* 连接答案 */
private joinAnswer(item) {
let answer: String = "";
item.forEach(element => {
answer += " " + element.type + " " + element.subject
+ " " + element.explain + " " + element.amount + "<br>";
})
return answer;
//replace只对第一个起作用,这个也是试了很多次才知道的
// let answer: String = "";
// item.forEach((element,indeX) => {
// let loanlistData = element.type.split();
// element.type = element.type.replace(" ","");
// element.type = element.type.replace(" ","");
// answer += " " + element.type + "" + element.subject
// + " :" + element.explain + "" + element.amount + " ";
// })
// return answer;
}
以上是大佬教程为你收集整理的【angular】ts截取字符串、json全部内容,希望文章能够帮你解决【angular】ts截取字符串、json所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。