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.

asked Apr 30, 2017 at 5:35
3
  • You are actually getting an Object from getElementsByClassName. Array and Object are two different types. Commented Apr 30, 2017 at 5:43
  • stackoverflow.com/questions/11064562/… check this stackoverflow question & answer Commented Apr 30, 2017 at 5:48
  • Do you want to delete the html elements from DOM? Commented Apr 30, 2017 at 6:13

3 Answers 3

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

answered Apr 30, 2017 at 5:41
Sign up to request clarification or add additional context in comments.

3 Comments

If you can use ES2015/ES6 then Array.from is your friend
true :-), although it is hard to google for ... when you see it in code and don't have a clue what it's for g
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.

answered Apr 30, 2017 at 5:41

Comments

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)
answered Apr 30, 2017 at 5:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.