问题:输入正确的syllables
后,我的输入字段应该变成绿色按钮。但是问题是:它通过JSON的长度与用户插入的内容来做到这一点,但是当我的数组仅包含2个单词而不是4个单词时,它仍然总共需要4个单词。例如:我的数组中有2个单词,并且数组中有2个空字符串,我被迫按Enter键两次以达到所需数组的长度。如何使它仅根据数组中的单词而不是空字符串检查长度?
这是我的JSON文件之一的样子:
{
"main_object": {
"id": "5",
"getExerciseTitle": "TestFor",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
"title": "TestFor",
"language": "nl_NL",
"exercises": [
{
"word": "test",
"syllables": [
"test01",
"test02",
"test03",
""
]
},
{
"word": "tesst",
"syllables": [
"test11",
"test12",
"",
""
]
}
]
},
"dataType": "json"
}
}
函数的创建方式,不必介意增量计数器等。它们已经被声明。
function createExercise(json) {
const exercises = json.main_object.main_object.exercises;
var exerciseArea = $('<div/>', {
id: 'exerciseField',
'class': 'col-md-12'
});
$.map(exercises, function (exercise, i) {
var exer = $('<div/>', {
'class': 'row form-group',
'id': +idRow++
})
var colLeft = $('<div/>', {
'class': 'col-md-3'
});
var row = $('<div/>', {
'class': 'row'
});
var audCol = $('<div>', {
class: 'col-md-3 audioButton'
}).append(getAudioForWords());
var wordCol = $('<div>', {
class: 'col-md-9 ExerciseWordFontSize exerciseWord',
'id': 'wordInput[' + ID123 + ']', // note to self: the brackets will need to be escaped in later DOM queries
text: exercise.word
});
row.append(audCol, wordCol);
colLeft.append(row);
var sylCol = $('<div>', {
class: 'col-md-9'
});
var sylRow = $('<div/>', {
class: 'row syll-row'
});
var correctSylls = [];
$.map(exercise.syllables, function (syllable, j) {
// Code to check if the syllable exists and is not an empty string
if(!syllable){
// If it doesn't exist or is an empty string, return early without creating/appending elements
return;
}
var innerSylCol = $('<div/>', {
class: 'col-md-3 inputSyllables'
});
var sylInput = $('<input/>', {
'type': 'text',
'class': 'form-control syl-input',
'name': +c++,
'id': +idsyll++
}).on('keyup', function() {
var cValue = $(this).val();
if (cValue === syllable) {
correctSylls.push(cValue);
console.log(correctSylls);
}
if (exercise.syllables.length === correctSylls.length) {
$(this).closest('.syll-row').find('input.syl-input').addClass('btn btn-success').removeClass('form-control');
}
});
innerSylCol.append(sylInput);
sylRow.append(innerSylCol);
});
idsyll = 0;
sylCol.append(sylRow);
exer.append(colLeft, sylCol);
exerciseArea.append(exer);
});
return exerciseArea;
}
引起问题的部分:
var correctSylls = [];
var sylInput = $('<input/>', {
'type': 'text',
'class': 'form-control syl-input',
'name': +c++,
'id': +idsyll++
}).on('keyup', function() {
var cValue = $(this).val();
if (cValue === syllable) {
correctSylls.push(cValue);
console.log(correctSylls);
}
if (exercise.syllables.length === correctSylls.length) {
$(this).closest('.syll-row').find('input.syl-input').addClass('btn btn-success').removeClass('form-control');
}
});
因此,我在这里基本上有2个问题:1)以某种方式在没有明显原因的情况下复制数组中的单词?和2)在我的数组中仍然需要4个单词(空字符串也被视为长度)。但我不会将字符串为空,因为它们不包含单词。另一个问题是:为什么要在数组中复制单词?
答案 0 :(得分:0)
可以过滤数组以仅返回非空项目。然后遍历map()中的过滤数组
var syllables = [
"test11",
"test12",
"",
""
];
var filtered = syllables.filter(function(str) {
return str.trim()
});
console.log(filtered)