如何在数组对象中找到id然后在oop javascript中编辑和删除对象?

时间:2017-05-19 04:18:19

标签: javascript arrays oop

我编写演示学生管理。但是下面有问题,请帮我解决。

要求

  • 管理学生:添加,修改,删除
  • 使用控制台构建数据表。该表包含fiedls:id Name Age Point
  • 使用控制台呼叫功能
  • 列出所有学生有点> = 5;

这是我的代码:

//
function Student(id, name, age, point) {
  this.id = id;
  this.name = name;
  this.age = age;
  this.point = point;
}

Student.prototype = {
  setId: function (value) {
    this.id = value;
  },

  getId: function () {
    return this.id;
  },

  setName: function (value) {
    this.name = value;
  },

  getName: function () {
    return this.name;
  },

  setAge: function (value) {
    this.age = value;
  },

  getAge: function () {
    return this.age;
  },

  setPoint: function (value) {
    this.point = value;
  },

  getPoint: function () {
    return this.point;
  },

};

function StudentController(student) {
  this.listStudent = [];
  this.id = 1;
}

StudentController.prototype = {
  addNew: function (name, age, point) {
    var student = new Student(this.id, name, age, point);
    this.listStudent.push(student);
    this.id += 1;
    return student;
  },

这是函数查找ID。但它总是返回数组对象中的最后一个id。

  findId: function (id) {
    var student = null;
    for (var i = 0; i < this.listStudent.length; i++) {
      if (id = this.listStudent[i].getId()) {
        student = this.listStudent[i];
      }
    }

    return student;
  },

这是功能编辑学生。但它不能getId来自findId();

  editStudent: function (student) {
    var oldStudent = this.findId(student.getId());
    console.log('oldStudent', oldStudent);
    for (var x = 0; x < this.listStudent.length; x++) {
      if (oldStudent = this.listStudent[x].getId()) {
        this.listStudent[x] = student;
      }
    }
  },

此功能也editStudent()功能相同。

  deleteStudent: function (student) {
    var crrentStudent = this.findId(student.getId());
    for (var y = 0; y < this.listStudent.length; y++) {
      if (crrentStudent.getId() === this.listStudent[y]) {
        this.listStudent.splice(y, 1);
      }
    }
  },

此功能的学生排序点&gt; = 5.但看起来不行:(

  // find point student > = 5
  findByPoint: function (point) {
    var point = '';
    for (var i = 0; i < this.listStudent.length; i++) {
      if (this.listStudent[i].getPoint() >= point) {
        return point;
      }
    }
  },

  showStudent: function () {
    console.table(this.listStudent);
  },
};

var studentController = new StudentController();
studentController.addNew("Hanh", 20, 8);
studentController.findId(1);
studentController.editStudent();
studentController.deleteStudent();

请帮我解决和解释。非常感谢 !!

1 个答案:

答案 0 :(得分:0)

问题非常简单,对于==中需要if condition的比较运算符,=用于分配和添加

更改findId()功能

findId: function (id) {
    var student = null;
    for (var i = 0; i < this.listStudent.length; i++) {
      if (id == this.listStudent[i].getId()) {    // change here
        student = this.listStudent[i];
      }
    }

    return student;
  },

editStudent函数

editStudent: function (student) {
    var oldStudent = this.findId(student.getId());
    console.log('oldStudent', oldStudent);
    for (var x = 0; x < this.listStudent.length; x++) {
      if (oldStudent == this.listStudent[x].getId()) {    //change here 
        this.listStudent[x] = student;
      }
    }
  },