1

I am getting the error tweetParentsArray.splice is not a function at injectFactCheck

function injectFactCheck(){
    var index = 5;
    var tweetParentsArray = document.getElementsByClassName("js-stream-tweet");
    if(tweetParentsArray.length == 0){return;}
    tweetParentsArray.splice(0, index);

When I console.log the tweetParentsArray it appears to be a normal array to me so I am not sure as to why this function would not exist on this object.

user7927331
  • 15
  • 1
  • 1
  • 6

3 Answers3

2

document.getElementsByClassName returns HTMLCollection which is not an array. You can use Array.prototype.slice.call(htmlCollection) to convert it to array and then perform further calculation with array.

function injectFactCheck(){
  var index = 5;
  var htmlCollection = document.getElementsByClassName("js-stream-tweet");
  var tweetParentsArray = Array.prototype.slice.call(htmlCollection);
  if (tweetParentsArray.length == 0){return;}
  tweetParentsArray.splice(0, index);
}

See more in this question: Most efficient way to convert an HTMLCollection to an Array

Community
  • 1
  • 1
Andrius
  • 947
  • 15
  • 22
0

getElementsByClassName return a HTMLCollection. It does not have splice method. You can convert the HTMLCollection to array to use the splice method.

var tweetParentsArray = Array.prototype.slice.call(document.getElementsByClassName("js-stream-tweet"))
    if (tweetParentsArray.length == 0) {
      return;
    }
    tweetParentsArray.splice(0, index)
brk
  • 48,835
  • 10
  • 56
  • 78
0

This in fact is an HTMLCollection, which is "array-like". See https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection for more details.

Omri Sivan
  • 339
  • 3
  • 8