简体   繁体   English

如何使用angular删除数组的指定索引内的元素

[英]How to remove elements inside specified index of array using angular

I need to remove some elements inside an array with specified index. 我需要删除具有指定索引的数组内的某些元素。 I am trying to use slice function for it but it is giving me undefined error. 我正在尝试使用切片功能,但它给了我未定义的错误。

This is my controller code: 这是我的控制器代码:

if($scope.startColm<$scope.endColm){
//Suppose here startColm value is 1 and endColm value is 4

  for(var j=0;j<rowCellData.length;j++){
  //rowCellData is an array. Attaching its image for reference

    for(var k=$scope.startColm;k<($scope.endColm-$scope.startColm);k++){

      var cellData=rowCellData[j].slice($scope.startColm,$scope.endColm)
      //Here i want to remove data inside rowCellData from specified index.
    }
  }
}

rowCellData consists data like this: rowCellData包含如下数据: 在此处输入图片说明

Inside each index there is some data which I need to fetch based on the specified startColm and endColm value. 在每个索引中,有一些数据需要基于指定的startColm和endColm值来获取。 Like here i want the data from 1st to 4th position for all the index of rowCellData. 像这里一样,我希望rowCellData的所有索引的数据从第1到第4位置。

在此处输入图片说明

Please suggest how to remove data inside array elements. 请建议如何删除数组元素内的数据。

You want the 'splice' method, not the 'slice' method. 您需要“拼接”方法,而不是“拼接”方法。 However, since you have objects inside the main array and not other arrays, you could use delete to remove the items: 但是,由于在主数组中有对象,而在其他数组中则没有对象,因此可以使用delete删除项目:

for (var i = $scope.startColm; i <= $scope.endColm; i++) { 
  delete rowCellData[j][i] 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM