简体   繁体   English

.push到对象中的数组

[英].push onto an array which is in an object

I am trying to .push() onto an array which is in an object. 我试图.push()到对象中的数组上。 How can I do this? 我怎样才能做到这一点?

I have an object called students{}. 我有一个名为“学生{}”的对象。 Each row of this contains a first name, last name, exam type and a subject for that exam type. 每一行都包含名字,姓氏,考试类型和该考试类型的主题。 I am trying to create an array (studentSubjectsByName) of all the subjects for this student for this exam type. 我正在尝试为此学生为此考试类型创建所有学科的数组(studentSubjectsByName)。 Then I want to put that array of subjects into another object called nameSearch{}. 然后,我想将该主题数组放入另一个名为nameSearch {}的对象中。

The info in the DB looks like this, 数据库中的信息如下所示:

The Database table looks like this 数据库表如下所示

> 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

(I can't post an image as I don't have 10 reps.) (我没有10次代表,因此无法发布图片。)

As you can see Mark has two subjects in A2. 如您所见,Mark在A2中有两个主题。

The following pushes subjects onto the array and results in the three subjects being elements in the arrays. 以下内容将主题推入数组,并导致三个主题成为数组中的元素。 There is an array for each subjects but each has three subjects. 每个主题都有一个数组,但每个主题都有三个主题。 Instead of the AP array having two and the A2 array having just one as it should be. 代替具有两个的AP阵列和应有的仅一个的A2阵列。 This is because the var studentSubjectsByName = []; 这是因为var studentSubjectsByName = []; is outside the for loop. 在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]);
        }
    }
}

The output is 输出是

userInputFirst: M userInputFirst:M

userInputFirst: Ma userInputFirst:马

userInputFirst: Mar userInputFirst:3月

userInputFirst: Mark userInputFirst:标记

num: 3 nameSearch[A2].subjects[0] is Further Mathematics num:3 nameSearch [A2] .subjects [0]是进阶数学

num: 3 nameSearch[A2].subjects[1] is Pure Mathematics num:3 nameSearch [A2] .subjects [1]是纯数学

num: 3 nameSearch[A2].subjects[2] is Calculus num:3 nameSearch [A2] .subjects [2]是微积分

num: 3 nameSearch[AP].subjects[0] is Further Mathematics num:3 nameSearch [AP] .subjects [0]是进阶数学

num: 3 nameSearch[AP].subjects[1] is Pure Mathematics num:3 nameSearch [AP] .subjects [1]是纯数学

num: 3 nameSearch[AP].subjects[2] is Calculus num:3 nameSearch [AP] .subjects [2]是微积分

With var studentSubjectsByName = []; 使用var studentSubjectsByName = []; inside the for loop the array is constantly being cleared. 在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]);
        }
    }
}

The output is. 输出是。

userInputFirst: M userInputFirst:M

userInputFirst: Mar userInputFirst:3月

userInputFirst: Mark userInputFirst:标记

num: 1 nameSearch[A2].subjects[0] is Pure Mathematics num:1 nameSearch [A2] .subjects [0]是纯数学

num: 1 nameSearch[AP].subjects[0] is Calculus num:1 nameSearch [AP] .subjects [0]是微积分

There should be two subjects in the AP array and one subject Calculus in the A2 array. AP阵列应有两个科目,A2阵列应有一个科目微积分。

The HTML is, 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>

I can not figure this out. 我不知道这一点。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Your question is not clear enough. 您的问题还不够清楚。 I'm answering your title directly. 我直接回答你的标题。

object.array.push(value);

Look at the updated code - i think you have a typo: 查看更新的代码-我认为您有错字:

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);
       }
    }
}

Here is the fiddle: https://jsfiddle.net/77er1a3r/ 这是小提琴: https : //jsfiddle.net/77er1a3r/

I can't get this to work using .innerHTML 我无法使用.innerHTML使它正常工作

var userInputFirst = document.getElementById('firstname').innerHTML;
var userInputLast = document.getElementById('lastname').innerHTML;
console.log("first: " + userInputFirst + " last: " + userInputLast);

But it does work, be it basically, with .value 但是它确实可以使用.value

var userInputFirst = document.getElementById('firstname').value;
var userInputLast = document.getElementById('lastname').value;
console.log("first: " + userInputFirst + " last: " + userInputLast);

The HTML is, HTML是

First name: Last name: 名:姓:

Is my use of .value instead of .innerHTML the type mentioned earlier? 我使用的是.value而不是.innerHTML的类型吗? Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM