简体   繁体   English

.splice() 不断删除数组的最后一项

[英].splice() keeps deleting the last item of an array

I made a function on my form where I can add multiple file inputs for multiple images by clicking a button, this is working as expected.我在表单上创建了 function,我可以通过单击按钮为多个图像添加多个文件输入,这按预期工作。 Now when I try to delete an input field with.splice it keeps deleting the last item of the array that my input fields are in, not the input field with the matching index.现在,当我尝试使用 .splice 删除输入字段时,它会不断删除我的输入字段所在的数组的最后一项,而不是具有匹配索引的输入字段。 I have been trying to fix this issue for hours now, I just can't seem to find a solution.我已经尝试解决这个问题几个小时了,我似乎无法找到解决方案。 I hope someone can tell me what I'm doing wrong.我希望有人能告诉我我做错了什么。

This is the method for adding a new input field:这是添加新输入字段的方法:

addInputField() {

    i++

    this.values.links.push({
      id: i,
      url: ''
    });
  }

this is the code for deleting an input field:这是删除输入字段的代码:

deleteInputField(index) {

    this.values.links.splice(index, 1);

    const items = this.values.links.filter(item => {

      return item.id > index
    });

    items.forEach(function (item) {
      item.id = item.id -1;
    });
  }

This is the button that deletes the input field:这是删除输入字段的按钮:

<v-icon
  medium
  v-if="link.id > 0"
  color="#FF0000"
  class="ma-4"
  @click="deleteInputField(link.id)"
>

links is an array of objects. links是一个对象数组。 You are not passing an index in that array, but an id inside the nested object.您不是在该数组中传递索引,而是在嵌套的 object 中传递一个 id。

<v-icon
  medium
  v-if="link.id > 0"
  color="#FF0000"
  class="ma-4"
  @click="deleteInputField(link.id)" // Change to index (mostly taken from v-for loop)
>

From the definition of the splice method, the first parameter is start:从splice方法的定义来看,第一个参数是start:

The index at which to start changing the array.开始更改数组的索引。 If greater than the length of the array, start will be set to the length of the array.如果大于数组的长度,start 将被设置为数组的长度。 If negative, it will begin that many elements from the end of the array (with origin -1, meaning -n is the index of the nth last element and is therefore equivalent to the index of array.length - n).如果为负数,它将从数组末尾开始那么多元素(原点 -1,意味着 -n 是倒数第 n 个元素的索引,因此等效于 array.length - n 的索引)。 If array.length + start is less than 0, it will begin from index 0.如果 array.length + start 小于 0,它将从索引 0 开始。

If you step through the code you will realise that the id you are passing is not an actual index of the link in the links array but the id of the link which is greater than the length of the array如果您单步执行代码,您将意识到您传递的 id 不是 links 数组中链接的实际索引,而是大于数组长度的链接 id

If you want to pass the id (link.id), just use this function.如果要传递 id(link.id),只需使用这个 function。

function deleteInputField(itemId){
    this.values.links = this.values.links.filter(item => {
        return item.id !== itemId;
    });
}

This will loop through the array and return all but the one that has the same id as the itemId passed.这将遍历数组并返回除与传递的 itemId 具有相同 id 的数组之外的所有数组。

otherwise, just pass in the index from the v-for loop in the @click handler否则,只需从 @click 处理程序中的 v-for 循环传入索引

deleteInputField(index) {
    this.values.links.splice(index, 1);
  }

The ID is not the index if you need to pass the index to the deleteInputField function you can delete the desired input field by this.values.links.splice(index, 1);如果需要将索引传递给 deleteInputField function,则 ID 不是索引,您可以通过this.values.links.splice(index, 1);

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

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