javascript - 从内部包含类对象的数组中删除特定项目

标签 javascript arrays knockout.js

我的问题

我正在尝试从数组中删除特定项目,但是,我的数组包含我无法获取句柄的其他对象。

我正在定义一个这样的“站”:

/* CLASS Station 
* @param  id       int   unique id this station
* @param  name     str   name of the station
* @param  location obj   the position of the station
*                        in the workflow diagram
*/
var Station = function(id, name, posX=null, posY=null) {

  this.id     = ko.observable(id || self.getUniqueId());
  this.name   = ko.observable(name);
  this.posX   = ko.observable(posX);
  this.posY   = ko.observable(posY);

};

所以我使用这个函数向我的数组添加了一个电台...

.
.
 self.addStation(new Station(76, "Receiving", 0, 10));

现在,我想知道如何通过传递名称从数组中删除,如下所示:

 self.removeStation("Receiving");

我想不通。我已经研究了这里的所有链接,但没有运气。

完整源代码

// CLASS Workflow
var Workflow = function(id, status){
    this.status = status || false;
    this.id = id;
}


/* CLASS Station 
* @param  id       int   unique id this station
* @param  name     str   name of the station
* @param  location obj   the position of the station
*                        in the workflow diagram
*/
var Station = function(id, name, posX=null, posY=null) {

  this.id     = ko.observable(id || self.getUniqueId());
  this.name   = ko.observable(name);
  this.posX   = ko.observable(posX);
  this.posY   = ko.observable(posY);

};



  // CLASS ViewModel 
  var ViewModel = function(workFlowId) {
    var self = this; // Scope Trick




    /*******************************
     * Observables
     *-----------------------------*/
    self.station = ko.observableArray();

    /*******************************
     * Initialize The Builder
     *-----------------------------*/
    self.workflowId = ko.observable();

    /*******************************
     * Arrays
     *-----------------------------*/

    self.workflow    = ko.observableArray();

    /*******************************
     * Actions
     *-----------------------------*/


    /* Method: initWorkflow
    *
    *  Desc: When the user gets to the builder
    *  page, we have to configure a Workflow.
    *  If they are loading a saved one, the ID
    *  will be passed. If they are starting a new
    *  one, we will give it a unique ID.
    *
    *  @param   int workFlowId  The id of the workflow
    *  @return  int workFlowId  The id is returned
    */  
    self.initWorkflow = function(workFlowId, status=false) {
      var id;
      if(!workFlowId){
        /* Call Function to generate unique ID */
        id = self.getUniqueId();
      } else {
        id = workFlowId;
      }

      /* Set ID */
      this.workflowId = id;
      this.workflow = new Workflow(id, status); 
    };



    /*------------------------------------------------------- 
    *  Method: addStation
    *
    *  Desc: Adds a station to current workflow
    *  @param    station   object  A station object
    *--------------------------------------------------------*/
    self.addStation = function(station){
      self.station.push(station);
    }


    /* Remove Station - */
    self.removeStation = function (Name) {
      for( var i = 0; i < self.station().length; i++){ 
        console.dir("In Remove Function: " + self.station()[i]);
     }
  }



    /*------------------------------------------------------- 
    *  Method: getUniqueId
    *
    *  Desc: Generates a random unique Id
    *  @returns  id   int   A unique random ID
    *--------------------------------------------------------*/
    self.getUniqueId = function(){
      var id = new Date().getTime();
      console.group("In Funtion: self.getUniqueId");
      console.log("Returned unique id of: " + id);
      console.groupEnd("In Funtion: self.getUniqueId");
      return id;
    }


    /* Start it up */
    self.initWorkflow(workFlowId);

    //------------------------
    //  UNIT TESTING
    //------------------------

    //........ STATION RELATED ..........................
    // 1. Add
    self.addStation(new Station(76, "Receiving", 0, 10));

    // 2. Remove
    self.removeStation("Receiving");


  }  // end ViewModel


  // Instantiate the ViewModel
  window.view_model = new ViewModel();

  // Away we go...
  ko.applyBindings(window.view_model);

我似乎无法获取数组中的名称:

// DON'T WORK
self.station()[i].Station.name

感谢您的浏览。 约翰

最佳答案

你可以使用函数来查找其索引,如下所示:

function arrayFirstIndexOf(array, predicate, predicateOwner) {
    for (var i = 0, j = array.length; i < j; i++) {
        if (predicate.call(predicateOwner, array[i])) {
            return i;
        }
    }
    return -1;
}

然后在您的 /* Remove Station - */u 中编辑代码,如下所示:

/* Remove Station - */
    self.removeStation = function (Name) {
    var index = arrayFirstIndexOf(self.station(), function(item){
        return item.name === Name;   
    });
    index > -1 && self.station.splice(index, 1);
}

希望这有帮助!

关于javascript - 从内部包含类对象的数组中删除特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57211758/

相关文章:

javascript - 如何使用 jQuery 查找和替换 HTML 或 XML 文档中两个标签之间的文本?

来自存储在数据库中的数组的php输出复选框然后显示选中如果选中

javascript - AngularJS 将字符串数组转换为 float 组

javascript - 使用 knockout 逐步加载视频元数据数组

javascript - 帮助我验证 url 是否应该接受 .me 域

javascript - HTML 5 Canvas addHitRegion 不适用于 iOS(未定义不是函数)

javascript - 为什么我的 jQuery 动画不能正常工作?

c - fopen 函数将垃圾放在文件路径名上

Knockout.js:绑定(bind)到复杂对象

knockout.js - koGrid 排序 - 服务器端分页