Object 在数组中,如何检查脚本?

Object 在数组中,如何检查脚本?,object,react-jsx,Object,React Jsx,因此,我将不断检索以下格式的对象: student: { "student_id": "12345", "location": "below", }, ] }, ] 谢谢你们,我会接受你们的回答和投票 像这样的事情应该可以做到: var students = []; function addStudent(student) { // Check if we already know about th

因此,我将不断检索以下格式的对象:

student: {
    "student_id": "12345",

                "location": "below",
            },
        ]
    },
]

谢谢你们,我会接受你们的回答和投票

像这样的事情应该可以做到:

var students = [];

function addStudent(student) {
  // Check if we already know about this student.
  var existingRecord = students.find(function (s) {
    return s.student_id === student.student_id;
  });

  var classInfo = {
    class_number: student.class_number,
    location: student.location
  };

  if (!existingRecord) {
    // This is the first record for this student so we construct
    // the complete record and add it.
    students.push({
      student_id: student.student_id,
      classes: [classInfo]
    });

    return;
  }

  // Add to the existing student's classes.
  existingRecord.classes.push(classInfo);
}
然后,您将按如下方式调用它:

addStudent({
    "student_id": "67890",
    "class_number": "abcd",
    "location": "below",
});
可运行的JSBin示例可用


更多信息请访问
Array.prototype.find

类似的内容应该可以实现以下目的:

var students = [];

function addStudent(student) {
  // Check if we already know about this student.
  var existingRecord = students.find(function (s) {
    return s.student_id === student.student_id;
  });

  var classInfo = {
    class_number: student.class_number,
    location: student.location
  };

  if (!existingRecord) {
    // This is the first record for this student so we construct
    // the complete record and add it.
    students.push({
      student_id: student.student_id,
      classes: [classInfo]
    });

    return;
  }

  // Add to the existing student's classes.
  existingRecord.classes.push(classInfo);
}
然后,您将按如下方式调用它:

addStudent({
    "student_id": "67890",
    "class_number": "abcd",
    "location": "below",
});
可运行的JSBin示例可用


更多信息,请访问
Array.prototype.find

此问题可以通过使用
学生id索引来解决。例如:

var sourceArray = [{...}, {...}, ...];

var result = {};

sourceArray.forEach(function(student){

    var classInfo = {
        class_number: student.class_number,
        location    : student.location
    };

    if(result[student.student_id]){

        result[student.student_id].classes.push(classInfo);

    } else {

        result[student.student_id] = {
            student_id  : student.student_id,
            classes     : [classInfo]
        }

    }
});


// Strip keys: convert to plain array

var resultArray = [];

for (key in result) {
    resultArray.push(result[key]);
}

您还可以使用包含对象的
result
格式,该格式由
student\u id
或普通数组
resultArray
索引,通过
student\u id
可以解决此问题。例如:

var sourceArray = [{...}, {...}, ...];

var result = {};

sourceArray.forEach(function(student){

    var classInfo = {
        class_number: student.class_number,
        location    : student.location
    };

    if(result[student.student_id]){

        result[student.student_id].classes.push(classInfo);

    } else {

        result[student.student_id] = {
            student_id  : student.student_id,
            classes     : [classInfo]
        }

    }
});


// Strip keys: convert to plain array

var resultArray = [];

for (key in result) {
    resultArray.push(result[key]);
}

您还可以使用包含对象的
result
格式,该格式由
student\u id
或普通数组
resultArray

索引。刚才尝试了一下,使用一个
student
对象,它可以成功添加,但当我尝试添加两个
student\u id
相同的
student\code>对象时,第一个
classInfo
对象被正确添加,第二个
classInfo
对象被添加到正确的位置,但其内部也有
student\u id
。问题可能出在哪里?我尝试了日志测试,但似乎找不到它。再次感谢Jabblabdisregard之前的评论。这是我的错。非常感谢你!接受了答案并投了赞成票。但是出于学习目的,
students.find(function(s))
在做什么?@JoKo很高兴它有帮助:-)您可以阅读更多关于
Array.prototype.find
的内容。只需尝试一下,通过一个
student
对象,它就能够成功添加,但是当我尝试添加两个具有相同
学生id
学生对象时,第一个
classInfo
对象被正确添加,第二个
classInfo
对象被添加到正确的位置,但其内部也有
学生id
。问题可能出在哪里?我尝试了日志测试,但似乎找不到它。再次感谢Jabblabdisregard之前的评论。这是我的错。非常感谢你!接受了答案并投了赞成票。但出于学习目的,学生.find(function(s))
在做什么?@JoKo很高兴它有帮助:-)你可以阅读更多关于
Array.prototype.find
的内容。谢谢。是的,前面的答案很有效,但是我的代码速度更快,因为它不使用任何像find()这样的方法。这对于大型阵列尤其重要。谢谢。是的,前面的答案很有效,但是我的代码速度更快,因为它不使用任何像find()这样的方法。这对于大型阵列尤其重要。