我正在尝试将.push()放到一个对象中的数组上。我怎么能这样做?
我有一个名为students {}的对象。其中的每一行都包含名字,姓氏,考试类型和该考试类型的主题。我正在尝试为此考试类型的学生创建所有科目的数组(studentSubjectsByName)。然后我想把那个主题数组放到另一个名为nameSearch {}的对象中。
数据库中的信息如下所示,
数据库表看起来像这样
> id exam subject year session result first last
>
> 186 A2 Further Mathematics 2015 January D Mark Murphy
>
> 185 A2 Pure Mathematics 2015 January A Mark Murphy
>
> 183 AP Calculus 2015 NULL 2 Mark Murphy
>
> 184 AP Chemistry 2015 NULL 5 David Smith
>
> 182 AP Calculus 2015 NULL 5 David Smith
(我无法发布图片,因为我没有10个代表。)
如你所见,马克在A2中有两个科目。
以下将主题推送到数组上,并导致三个主题是数组中的元素。每个科目都有一个阵列,但每个科目都有三个科目。而不是具有两个的AP阵列和仅具有一个应该是的A2阵列。这是因为var studentSubjectsByName = [];在for循环之外。
function checkStudent(){
var subjectSearch = {};
var nameSearch = {};
var userInputFirst = document.getElementById('firstname').value;
var userInputLast = document.getElementById('lastname').value;
var students = <?php echo json_encode($studentNames, JSON_PRETTY_PRINT); ?>;
var studentSubjectsByName = [];
for(var i = 0; i < students.length; i++){
console.log("userInputFirst: " + userInputFirst);
if(userInputFirst == students[i].first && userInputLast == students[i].last){
//console.log("First name: " + students[i].first + " Last name: " + students[i].last + " Exam: " + students[i].exam + " Subject: " + students[i].subject);
nameSearch[students[i].exam] = {first:students[i].first, last:students[i].last, subjects:studentSubjectsByName};//this clears the array
nameSearch[students[i].exam].subjects.push(students[i].subject);
//console.log(studentSubjectsByName);
//console.log(nameSearch);
}
}
for(var item in nameSearch){
var num = nameSearch[item].subjects.length;
for(var i=0; i<num; i++){
console.log("num: " + num + " nameSearch[" + item + "].subjects[" + i + "] is " + nameSearch[item].subjects[i]);
}
}
}
输出
userInputFirst:M
userInputFirst:Ma
userInputFirst:Mar
userInputFirst:标记
num:3 nameSearch [A2] .subjects [0]是Further Mathematics
num:3 nameSearch [A2] .subjects [1]是Pure Mathematics
num:3 nameSearch [A2] .subjects [2]是微积分
num:3 nameSearch [AP] .subjects [0]是Further Mathematics
num:3 nameSearch [AP] .subjects [1]是Pure Mathematics
num:3 nameSearch [AP] .subjects [2]是微积分
使用var studentSubjectsByName = [];在for循环中,数组不断被清除。
function checkStudent(){
var subjectSearch = {};
var nameSearch = {};
var userInputFirst = document.getElementById('firstname').value;
var userInputLast = document.getElementById('lastname').value;
var students = <?php echo json_encode($studentNames, JSON_PRETTY_PRINT); ?>;
for(var i = 0; i < students.length; i++){
var studentSubjectsByName = [];
console.log("userInputFirst: " + userInputFirst);
if(userInputFirst == students[i].first && userInputLast == students[i].last){
//console.log("First name: " + students[i].first + " Last name: " + students[i].last + " Exam: " + students[i].exam + " Subject: " + students[i].subject);
nameSearch[students[i].exam] = {first:students[i].first, last:students[i].last, subjects:studentSubjectsByName};//this clears the array
nameSearch[students[i].exam].subjects.push(students[i].subject);
//console.log(studentSubjectsByName);
//console.log(nameSearch);
}
}
for(var item in nameSearch){
var num = nameSearch[item].subjects.length;
for(var i=0; i<num; i++){
console.log("num: " + num + " nameSearch[" + item + "].subjects[" + i + "] is " + nameSearch[item].subjects[i]);
}
}
}
输出是。
userInputFirst:M
userInputFirst:Mar
userInputFirst:标记
num:1 nameSearch [A2] .subjects [0]是Pure Mathematics
num:1 nameSearch [AP] .subjects [0]是微积分
AP阵列中应该有两个主题,A2阵列中应该有一个主题。
HTML是,
<td class="left">
First name: <input type="input" name="last" id="firstname" onkeyup="checkStudent()" placeholder="first name">
</td>
<td class="left">
Last name: <input type="input" name="first" id="lastname" onkeyup="checkStudent()" placeholder="last name">
</td>
我无法弄清楚这一点。任何帮助,将不胜感激。感谢
答案 0 :(得分:0)
你的问题不够明确。我正在直接回答你的头衔。
var ft = SupportFragmentManager.BeginTransaction();
ft.AddToBackStack(fragInfo.Tag);
ft.Commit();
答案 1 :(得分:0)
查看更新的代码 - 我认为你有一个错字:
function checkStudent(){
var nameSearch = {};
var subjectSearch = {};
var userInputFirst = document.getElementById('firstname').innerHTML;
var userInputLast = document.getElementById('lastname').innerHTML;
var students = [{ first: "first1", last: "last1", exam: "exam1", subject: "subject1"}, { first: "first2", last: "last2", exam: "exam2", subject: "subject2"}];
for(var i = 0; i < students.length; i++){
var studentSubjectsByName = [];
if(userInputFirst == students[i].first && userInputLast == students[i].last){
console.log("First name: " + students[i].first + " Last name: " + students[i].last + " Exam: " + students[i].exam + " Subject: " + students[i].subject);
studentSubjectsByName.push(students[i].subject);
nameSearch[students[i].exam] = {first:students[i].first, last:students[i].last, subjects:studentSubjectsByName};
nameSearch[students[i].exam].subjects.push(students[i].subject);
console.log(studentSubjectsByName);
console.log(nameSearch);
}
}
}
答案 2 :(得分:0)
我无法使用.innerHTML
来解决这个问题var userInputFirst = document.getElementById('firstname').innerHTML;
var userInputLast = document.getElementById('lastname').innerHTML;
console.log("first: " + userInputFirst + " last: " + userInputLast);
但它确实有用,基本上是.value
var userInputFirst = document.getElementById('firstname').value;
var userInputLast = document.getElementById('lastname').value;
console.log("first: " + userInputFirst + " last: " + userInputLast);
HTML是,
名字: 姓:
我是否使用.value代替.innerHTML前面提到的类型? 感谢