简体   繁体   中英

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. However, I am having an issue with an error " myFunc error = TypeError: Object doesn't support property or method '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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