簡體   English   中英

如何在數組對象中找到ID,然后在oop JavaScript中編輯和刪除對象?

[英]How to find id in array object then edit and delete a object in oop javascript?

我編碼演示學生管理。 但是下面有問題,請幫助我解決。

要求

  • 管理學生:添加,編輯,刪除
  • 使用控制台建立數據表。 該表包含以下字段: 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;
  },

這是函數find 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);
      }
    }
  },

學生的此函數排序點> =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();

請幫我解決和解釋。 非常感謝 !!

問題非常簡單,對於比較運算符, 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;
      }
    }
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM