我有一个由“|”分隔的字符串组成的数组:
LOG: 380|2|choice|Question_8_13240_1351170102936|||incorrect|2012-10-25T15:01:13|1|<ln>380|2|choice|Question_10_13280_1351170102995|||incorrect|2012-10-25T15:01:17|1|
并且需要从每个字符串中获取Question_8_13240_1351170102936以便我可以看到是否存在任何重复的问题,因此在第三次出现“|”之后的字符串在第4次之前
构建数组的代码:
AssessmentManager.prototype.submit = function () {
assessData = "";
assessmentItemsLength = this.assessmentItems.length;
for (i = 0; i < this.assessmentItems.length; i++) {
if (!this.assessmentItems[i].getSubmitted()) {
assessmentFlatDataItem = this.assessmentItems[i].getFlatData();
if (assessmentFlatDataItem.length > 0) {
if (assessData.length > 0)
assessData += "<ln>";
assessData += assessmentFlatDataItem;
}
}
}
}
然后GetFlatdata:
AssessmentItem.prototype.getFlatData = function () {
result = _userScormModuleId + "|";
if (this._interactionIdx)
result += this._interactionIdx;
result += "|";
if (this._interactionType)
result += this._interactionType
result += "|";
if (this._interactionId)
result += this._interactionId
result += "|";
if (this._learnerResponse)
result += this._learnerResponse
result += "|";
if (this._correctResponse)
result += this._correctResponse
result += "|";
if (this._result)
result += this._result
result += "|";
if (this._timeStamp)
result += this._timeStamp
result += "|";
if (this._weighting)
result += this._weighting
result += "|";
if (this._description)
result += this._description
return result;
}
我将如何实现这一目标? 感谢
我现在有这个:
learnerResponsesArray.push({
key: j,
value: this.assessmentItems.split('<ln>')
});
然后遍历learnerResponsesArray?
答案 0 :(得分:2)
var cells = mystring.split("|");
// nth item...
var myitem = cells[n];
但正如@ mori57所说,为什么要使用这种格式而不是JSON?
答案 1 :(得分:0)
试试这个
str = "LOG: 380|2|choice|Question_8_13240_1351170102936|||incorrect|2012-10-25T15:01:13|1|<ln>380|2|choice|Question_10_13280_1351170102995|||incorrect|2012-10-25T15:01:17|1| "
console.log(str.split(":")[1].split("|")[3])
答案 2 :(得分:0)
如果你需要3d和第4 |
之间的值,你可以使用:
var fourthelement = [yourstring].split(/\|/,4).pop();