1

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.

6
  • 5
    That's not an Array; it's an HTMLCollection. Commented Dec 1, 2016 at 15:52
  • 2
    Try this ► var actualArray = [].slice.call(editsArray); , taken from this post then change your code to process the actualArray instead. That might work. Commented Dec 1, 2016 at 15:54
  • 3
    Or the new Array.from(editsArray). You can use .filter() too. editsArray = Array.from(editsArray).filter(function(el) { return el.name != 'problem_box' }) Commented Dec 1, 2016 at 15:56
  • @François Wahl :: In the context of the above loops, would that method of removal actually work? There could be scenarios where we may need to enter that if statement more than once. Commented Dec 1, 2016 at 16:11
  • @squint Using your suggestion I've done this, renaming the from "editsArray" variable to something including "collection" to be proper. var allEditsArray = Array.from(collectionUnclean).filter(function(el) { return el.name != 'problem_box' }) When doing this I'm getting an error "object doesn't support property or method 'from' " Commented Dec 1, 2016 at 16:19

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.