我想删除重复项而不在Javascript中排序

时间:2023-01-09 07:37:39

Javascript:

使用Javascript:

function Add(){
    var mydiv = document.getElementById("mydiv");
                var images = mydiv.getElementsByTagName("img");
                var dydiv = document.createElement("div");        


                for (var i = 0; i < images.length; i++) {              

                        var elem = document.createElement("img");
                        elem.setAttribute("src", images[i].src);
                        elem.setAttribute("height", "100%");
                        elem.setAttribute("width", "100%");
                        dydiv.appendChild(elem);

                    }
}

Html:

HTML:

<input type="button" onclick="Add()" value="Add"/>

    <div id="mydiv">
      <asp:Repeater id="rpt" runat="server">
        <itemtemplate>
                <div >
                    <img src='<%#Eval("img")%>' alt="No Image" />
                </div>

         </temtemplate>
       </asp:Repeater> 


         </div>

Here I'm getting the duplicate values in Images array object. As there are 4 images, I am getting 8 images.(btw I don't understand why I am getting duplicates :-( ) Now I want to remove the duplicates without sorting. plz help me....

这里我在Images数组对象中获取重复值。由于有4张图片,我得到8张图片。(顺便说一句我不明白为什么我会得到重复:-()现在我想要删除重复项而不进行排序.plz帮助我....

2 个解决方案

#1


1  

Hope this logic to remove duplicates from an array might help. I'm not saying to copy this and paste this. I just want to share the logic, and the rest I think you can figure it out.

希望这个逻辑从数组中删除重复可能会有所帮助。我不是说复制它并粘贴它。我只是想分享逻辑,其余我认为你可以弄明白。

var items = ["milk","bottle","mobile","cups","milk","screen","table", "toothpaste"];
var filteredItems= [];
$.each(items, function(i, element){
    if($.inArray(element, filteredItems) === -1) filteredItems.push(element);
});

The array :filteredItems will contain uniquevalues without sorting the original array..
Hope that serves you.

数组:filteredItems将包含uniquevalues而不对原始数组进行排序。希望能为您服务。

#2


0  

This may be because as you append images to your document, the images HTMLCollection (images) increases in size.

这可能是因为当您将图像附加到文档时,图像HTMLCollection(图像)的大小会增加。

If I do var images = document.getElementsByTagName('img');, I get an HTMLCollection containing all the elements that I looked for.

如果我做var images = document.getElementsByTagName('img');,我得到一个HTMLCollection,其中包含我查找的所有元素。

If I then go and do:

如果我然后去做:

var newImage = document.createElement('img');
document.getElementsByTagName('body')[0].appendChild(newImage);

The HTML collection now contains the new image as well.

HTML集合现在也包含新图像。

What you may be trying to accomplish is changing the height, width and src of the image that already exists. In that case, you wouldn't have to create a new element and then append it, you could just loop over the HTMLCollection and change the height, width and src of the element in the HTMLCollection.

您可能要尝试完成的是更改已存在的图像的高度,宽度和src。在这种情况下,您不必创建新元素然后追加它,您可以循环遍历HTMLCollection并更改HTMLCollection中元素的高度,宽度和src。

#1


1  

Hope this logic to remove duplicates from an array might help. I'm not saying to copy this and paste this. I just want to share the logic, and the rest I think you can figure it out.

希望这个逻辑从数组中删除重复可能会有所帮助。我不是说复制它并粘贴它。我只是想分享逻辑,其余我认为你可以弄明白。

var items = ["milk","bottle","mobile","cups","milk","screen","table", "toothpaste"];
var filteredItems= [];
$.each(items, function(i, element){
    if($.inArray(element, filteredItems) === -1) filteredItems.push(element);
});

The array :filteredItems will contain uniquevalues without sorting the original array..
Hope that serves you.

数组:filteredItems将包含uniquevalues而不对原始数组进行排序。希望能为您服务。

#2


0  

This may be because as you append images to your document, the images HTMLCollection (images) increases in size.

这可能是因为当您将图像附加到文档时,图像HTMLCollection(图像)的大小会增加。

If I do var images = document.getElementsByTagName('img');, I get an HTMLCollection containing all the elements that I looked for.

如果我做var images = document.getElementsByTagName('img');,我得到一个HTMLCollection,其中包含我查找的所有元素。

If I then go and do:

如果我然后去做:

var newImage = document.createElement('img');
document.getElementsByTagName('body')[0].appendChild(newImage);

The HTML collection now contains the new image as well.

HTML集合现在也包含新图像。

What you may be trying to accomplish is changing the height, width and src of the image that already exists. In that case, you wouldn't have to create a new element and then append it, you could just loop over the HTMLCollection and change the height, width and src of the element in the HTMLCollection.

您可能要尝试完成的是更改已存在的图像的高度,宽度和src。在这种情况下,您不必创建新元素然后追加它,您可以循环遍历HTMLCollection并更改HTMLCollection中元素的高度,宽度和src。