【问题标题】:Typescript Splice an Item from an array nested within an arrayTypescript 从嵌套在数组中的数组拼接项目
【发布时间】:2017-06-10 23:07:57
【问题描述】:

我有一个嵌套在另一个项目数组中的项目数组。我试图删除嵌套数组中的特定项目。我有下面的代码。

removeFromOldFeatureGroup() {
for( let i= this.featureGroups.length-1; i>=0; i--) {
  if( this.featureGroups[i].featureGroupId == this.oldFeatureGroupId)
    for( let z= this.featureGroups[i].features.length-1; z>=0; z--) {
      if( this.featureGroups[i].features[z].featureId == this.featureId)
        this.transferedFeature = this.featureGroups[i].features[z];
        this.featureGroups[i].features[z].splice(z, 1);
        return;
    }
}
}

当我开始拼接时,会抛出一个错误,说拼接不是一个函数。如何解决这个问题?另外,this.transferedFeature 是我需要删除的正确的。

【问题讨论】:

    标签: javascript arrays typescript multidimensional-array


    【解决方案1】:

    应该是:

    this.featureGroups[i].features.splice(z, 1);
    

    因为this.featureGroups[i].features 是数组中的数组(根据您的描述)。

    【讨论】:

      【解决方案2】:

      我认为您将 z 处的元素误认为是一个数组,这是正确的代码

      removeFromOldFeatureGroup() {
          for( let i= this.featureGroups.length-1; i>=0; i--) {
          if( this.featureGroups[i].featureGroupId == this.oldFeatureGroupId)
              for( let z= this.featureGroups[i].features.length-1; z>=0; z--) {
              if( this.featureGroups[i].features[z].featureId == this.featureId)
                  this.transferedFeature = this.featureGroups[i].features[z];
                  this.featureGroups[i].features.splice(z, 1);
                  return;
              }
          }
          }
      

      【讨论】:

        【解决方案3】:

        这都是关于索引的使用。

        outerArr = [];
        inner= [1,121,13];
        outerArr.push(inner);
        console.log(outerArr);
        outerArr[0].splice(0,1)
        console.log(outerArr)
        

        【讨论】:

          猜你喜欢
          • 2020-12-16
          • 2015-01-06
          • 2017-06-11
          • 2015-11-26
          • 2012-07-16
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多