简体   繁体   English

我无法从数组中删除项目

[英]I can't remove an item from an array

This is what I found online on how to remove an item from an array. 这是我在网上找到的有关如何从阵列中删除项目的信息。 I use splice, but when I open the console it says undefined. 我使用接头,但是当我打开控制台时,它说未定义。 How can I remove the item from the array with jquery or javascript? 如何使用jquery或javascript从数组中删除项目?

Thanks. 谢谢。

var images;

    function readURL2(input, where) {
        images = input;
        var counter = 0;
        var html = "";
        if (input.files && input.files[0]) {
            for (var i = 0; i < input.files.length; i++) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    html += "<tr><td><img src='" + e.target.result + "' height='100'></td><td><button type='button' class='btn btn-danger' onclick='removeImg(" + counter + ")'><span class='fa fa-remove'></span> Remove</button></rd></tr>";

                    $(where).html(html);
                    counter++;
                }

                reader.readAsDataURL(input.files[i]);
            }
        }
    }

    function removeImg(imgIndex) {
        images.files.splice(imgIndex, 1);
        console.log(images.files);
        readURL2(images, "#miltiim");
    }

// Start with an initial array //从初始数组开始

var array = ["a", "b", "c"];

// Find and remove item from an array //查找并从数组中删除项目

var i = array.indexOf("b");
if(i != -1) {
    array.splice(i, 1);
}

Of course if you'd like to remove multiple occurrences of the same string/number, you'll need add a bit more logic: 当然,如果您想删除同一字符串/数字的多次出现,则需要添加更多逻辑:

for(var i = array.length-1; i--;){
    if (array[i] === "b") array.splice(i, 1);
}

You may be thinking that the filter method would work... 您可能会认为filter方法会起作用...

array.filter(function(i) {
    return i != "b"
});

...but that will return a new array, thus not modifying the original. ...但是这将返回一个新数组,因此不会修改原始数组。

Removing a given value from an array isn't too difficult of a task when you have a reliable snippet nearby! 当附近有可靠的代码段时,从数组中删除给定值并不是一件容易的事!

- -

source : http://davidwalsh.name/remove-item-array-javascript 来源: http : //davidwalsh.name/remove-item-array-javascript

The problem is that you supply the initial index of each image to the remove function, but removing an image changes the index within the images array: 问题是您将每个图像的初始索引提供给remove函数,但是删除图像会更改images数组中的索引:

Initial:
         index
image0   0
image1   1
image2   2

removeImg("1") will remove image1, you will have: removeImg("1")将删除image1,您将拥有:

         index
image0   0
image2   1

removeImg("2") to remove image2 will not work as its index now is 1. removeImg("2")删除image2将不起作用,因为它的索引现在为1。

What you should do, and will not require to generate the images each time, is to set an id for each tr containing an image and remove it using jquery: 您应该做的,并且不需要每次都生成图像的方法是,为每个包含图像的tr设置一个ID,并使用jquery将其删除:

Html: HTML:

<tr id='row-image-"+counter+"'>
<td>
<img src='" + e.target.result + "' height='100'>
</td>
<td>
<button type='button' class='btn btn-danger'
        onclick='removeImg(" + counter + ")'>
<span class='fa fa-remove'></span> Remove</button>
</rd>
</tr>

JS: JS:

function removeImg(imgIndex) {
    $('#row-image-'+imgIndex).remove();
}

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

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