【问题标题】:Why Splice removes only last element when array elements are the same为什么当数组元素相同时,Splice 只删除最后一个元素
【发布时间】:2020-02-22 08:59:09
【问题描述】:

转到这个StackBlitz Link (Not Working)

转到这个StackBlitz Link (Working Fine)

我的问题是关于Array.splice。 当数组数据不同但数组数据属于同一类型时,Array.splice 工作正常,Array.splice 仅删除最后一个索引。在这种情况下,splice 会忽略提供的索引以从数组中删除元素。

两个例子都有共同的 HTML 模板

<div class="container">
    <table class="table table-dark table-striped table-hover">
        <thead>
           <tr>
               <td scope="col"> Index </td>
               <td scope="col"> Value </td>
               <td scope="col"> Action </td>
           </tr>
        </thead>
        <tbody>
            <tr *ngFor="let row of rows; let i = index"  [@itemAnim]>
                <td> {{i}} </td>
                <td>{{row}}</td>
                <td><button class="btn btn-danger" (click)="deleteRow(i)">Delete</button></td>
           </tr>
        </tbody>
    </table>
    <button class="btn btn-primary " (click)="addRow()">Add row</button>
</div>

查看此示例按预期工作:

index = 1;
rows = [1]; 

addRow() {   
   this.rows.length > 0 ?  this.index ++ : this.index = 1;
   this.rows.push(this.index)
}

deleteRow(row: number) {
   return this.rows.length > 0 ? this.rows.splice(row, 1) : this.index = 1;
}

这不起作用:

rows = [1]; 

addRow() {   
  this.rows.push(1)
}

deleteRow(row: number) {
   this.rows.splice(row, 1) ;
}

在上面的代码中,当点击addButton 时,我只推送1。并且删除按钮仅从最后一个索引中删除,而不是提供行号索引。

我的问题是为什么会这样?上面的 stackBlitz 链接显示了同样的歧义。

【问题讨论】:

  • 我认为这里有一个误解。尝试每次推送一个新号码,这样您就可以看到被删除的内容。 this stackblitz 是否消除了您的困惑? (从你的分叉而来)
  • 你是对的。我已经在See this example working as expected 部分用粗体字提到了这一点。我的问题是.. 第二部分 And this is not working: 为什么推送相同的元素在 splice 中不起作用。
  • 不工作是什么意思?能否详细说明。分叉的 stackblitz 使用你提到的代码不起作用。
  • 如果你总是推送同一个元素,你怎么知道我的意思是哪个被删除了。
  • 你将哪个参数传递给deleteRow?尽管非现场参考可能会有所帮助,但请确保所有相关信息都在您的问题中,而不是在链接后面。

标签: javascript angular typescript


【解决方案1】:

因为你实际上是在移除选中的索引,但是 Angular 并没有任何关于值之间差异的信息,我们来看一个例子:

我们有这个数组:[1, 1, 1, 1] 然后你删除第二个项目,然后删除之前的角度会看到:

[1, 1, 1, 1]

之后:

[1, 1, 1]

此时angular只知道少了一项,不知道wish item,那么angular只删除最后一项。

您需要确保传递不同的值或引用(推荐),然后 Angular 将知道如何区分数组项。

你可以通过使用像{ data: 1 }这样的对象来修复它,你的数组看起来会是:

[
  { data: 1 },
  { data: 1 },
  { data: 1 },
  { data: 1 }
]

为什么会这样?因为引用不一样(Value vs Reference Types),即使数据相同,angular 也会看到不同的引用。

这对你有用:

rows = [{ data: 1 }]; 

addRow() {   
  this.rows.push({ data: 1 });
}

deleteRow(row: number) {
  this.rows.splice(row, 1);
}

在这个例子中,你总是传递一个带有自己引用的新对象。


这另一个例子不起作用

sameReference = { data: 1 };

rows = [this.sameReference]; 

addRow() {   
  this.rows.push(this.sameReference);
}

deleteRow(row: number) {
  this.rows.splice(row, 1);
}

因为sameReference 变量存储的是引用,而不是对象,并且 angular 不会(再次)知道项目之间的差异,另外,当您更改一个元素的数据值时,所有元素都会获得该值,因为有数组中只有一个对象被引用了 N 次。


这也行

sameReference = { data: 1 };

rows = [{ ...this.sameReference }]; 

addRow() {   
  this.rows.push({ ...this.sameReference });
}

deleteRow(row: number) {
  this.rows.splice(row, 1);
}

因为这里我们将对象的值复制到具有不同引用的新对象中。

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 2021-04-19
    • 2017-01-04
    • 2019-08-03
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多