简体   繁体   English

JS-从数组中删除元素(使用拼接的问题)

[英]JS - Removing Elements from Array (Issues using Splice)

I'm working to build a edit viewer for my personal website. 我正在为我的个人网站构建一个编辑查看器。 I have an array which I use to gather all of the textboxes/textareas from the page (based on their class) . 我有一个数组,可以用来从页面中收集所有文本框/文本区域(基于它们的类) Then through the usage of buttons I let the user move from edit to edit. 然后,通过使用按钮,我让用户从一个编辑移到另一个。 When a button is pressed I need to iterate through the array of "edited" objects. 当按下按钮时,我需要遍历“已编辑”对象的数组。

Using the great tips I found on a previous Overflow thread, I'm looping from the top of the array back through. 使用在上一个Overflow线程中发现的出色技巧,我从数组顶部开始循环。 However, I am having an issue with an error " myFunc error = TypeError: Object doesn't support property or method 'splice' ". 但是,我遇到了一个错误问题“ myFunc error = TypeError:对象不支持属性或方法'splice' ”。 What have I done incorrectly? 我做错了什么?

var editsArray = document.getElementsByClassName("recent_change");

    // go backwards through the array
    for(var i = editsArray.length -1; i >= 0 ; i--){
        // check to ensure it is not a 'problem_box'
        if(editsArray[i].name == 'problem_box'){
            // remove that item from array
            editsArray.splice(i, 1);
        }
    }

I know that my way of collecting elements into the array works as my code to iterate through these edits was previously working. 我知道我将元素收集到数组中的方式可以正常工作,因为我以前遍历这些编辑的代码在工作。 I'm just going back now to "clean up" what is gathered into my array and am running into the error mentioned above. 我现在回去“清理”收集到数组中的内容,并遇到上述错误。

I am not wanting remove the objects collected from the actual page, I am simply wanting to remove the reference to those objects out of my array. 我不想删除从实际页面收集的对象,我只是想从数组中删除对这些对象的引用

const editsArray = Array.from(document.getElementsByClassName("recent_change")).
                     filter((node) => node.name !== 'problem_box')

If You can't use ES6, replace Array.from with Array.prototype.slice, and arrow function with normal function. 如果您不能使用ES6,则将Array.from替换为Array.prototype.slice,并将箭头函数替换为普通函数。

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

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