为什么我的函数只返回数组中的第一个对象

时间:2017-05-23 07:28:22

标签: angular typescript

我正在为学生组织者网络应用编写笔记模块。我写了一个函数,通过它的ID来获取一个对象,但它没有正常工作,或者我的代码的另一部分出了问题,但是我无法理解它。

此功能应返回类别和主题的名称。不知怎的,它只返回类别&中的第一个名字。 subject Array。

我的尝试:

findMyObjectByID = function (objArray, id) {
for (const obj of objArray){
  if (+obj._ID === +id) {
    console.log('--------------------');
    console.log(obj._ID + ' | ' + id );
    console.log(obj._Name);
    console.log('--------------------');
    return obj._Name;
  } else {
    console.log('--------------------');
    console.log(obj._ID + ' | ' + id );
    console.log('ELSE');
    console.log('--------------------');
    return 'undefined';
  }
}
};

我在没有View的空白项目中测试了该功能。这里没问题。 https://pastebin.com/n2JZhMGW

HTML / Angular - form&图
我选择一个类别和一个主题。注释类别有不同的选项。 1& 2是截止日期和主题输入字段,3是标题和文本。所以这是我的HTML / Angular表单和视图。

<input type="text" name="title" [(ngModel)] = "Title" placeholder="Titel">
<select name="Category" [(ngModel)] = "Category">
  <option *ngFor="let category of Categorys" value="{{category._ID}}">{{category._Name}}</option>
</select>

<div [ngSwitch]="Category">
  <div *ngSwitchCase="1">
    <select name="subject" [(ngModel)] = "Subject">
      <option *ngFor="let subject of Subjects" value="{{subject._ID}}">{{subject._Name}}</option>
    </select>
    <input type="text" name="deadline" [(ngModel)] = "Enddate" placeholder="Deadline eintragen">
  </div>
  <div *ngSwitchCase="2">
    <select name="subject" [(ngModel)] = "Subject">
      <option *ngFor="let subject of Subjects" value="{{subject._ID}}">
{{subject._Name}}</option>
    </select>
    <input type="text" name="deadline" [(ngModel)] = "Enddate" placeholder="Deadline eintragen">
  </div>
</div>
<hr>
<div *ngIf="Category == 1 || Category == 2 || Category == 3">
  <input type="text" name="note" [(ngModel)] = "Note" placeholder="Notiz">
</div>
<hr>
  <button (click) = "add()">Save me</button>
<hr>
<div class="noteBox">
  <h1>Notizen </h1>
  <div class="note"  *ngFor="let note of Notes">
    <h2>{{note._Title}}</h2>
    <span>{{note._DateCreated}} | {{note._CategoryName}}</span>
    <span>Fach: {{note._SubjectName}} | Ablaufdatum: {{note._Enddate}}</span>
    <hr>
    <article>
      {{note._Content}}
    </article>
  </div>
</div>

打字稿 - 数据&amp;对象创建
选择框的值是主题和类别对象的ID,它们稍后将表示数据库ID。这就是我创建笔记对象实例的方法。

add = function () {
// id, title, content, subjectID, subjectName, userID, userName, categoryID, categoryName, dateCreated, enddate
this.Notes.push(new Note(0, this.Title, this.Note, this.Subject, this.findMyObjectByID(this.Subjects, this.Subject), 0, 'admin', this.Category, this.findMyObjectByID(this.Categorys, this.Category), '2017-05-18', this.Enddate));
this.Note = '';
this.Title = '';
this.Category = 0;
this.Enddate = '';
this.Subject = 0;
};

如果它有用:这是我的数据。由于我没有数据源,因此数据是硬编码的。

   constructor() {
    this.addSubjects();
    this.addCategorys();
  }

  addSubjects = function () {
    this.Subjects.push(new Subject(89, 'math'));
    this.Subjects.push(new Subject(16, 'german'));
    this.Subjects.push(new Subject(45, 'english'));
    this.Subjects.push(new Subject(89, 'ped'));
  };

  addCategorys = function () {
    this.Categorys.push(new Category(45, 'homework'));
    this.Categorys.push(new Category(13, 'reminder'));
    this.Categorys.push(new Category(27, 'fastnote'));
  };

完成app.component.ts文件:https://pastebin.com/LrUWYgpX

1 个答案:

答案 0 :(得分:1)

如果第一个id不等于objArray中的第一个元素,则for循环将退出,因为循环中有return 'undefined'

我认为正确的方法是将return语句移出循环

  findMyObjectByID = function (objArray, id) {
    for (const obj of objArray) {
      if (+obj._ID === +id) {
        console.log(obj._Name);
        return obj._Name;
      }
    }
    return 'undefined';
  }