1seanr
1seanr

Reputation: 706

Getting error .append is not a function

I am trying to split up a string on /* and then to split up those segments on */

So that I can separate out all of the comments as I want this code to be able to take all of the comments out of the string and then put it back together.

The problem is though I keep getting this .append error which I am pretty sure is because I have made a silly syntax error but I am struggling to find it and any help would be greatly appreciated.

JS

contents = "if for /* else */ . = == === /* return */ function"

var start = /\/\*/gi;
var end = /\*\//gi;
var commentsRemovedSec2 = [];

var commentsRemovedSec1 = contents.split(start);
console.log(commentsRemovedSec1);

for (var i = 0; i < commentsRemovedSec1.length; i++) {
  var z = ""
  var x = commentsRemovedSec1[i]
  var y = x.split(start)
  z = y[0]
  commentsRemovedSec2.append(z);
};

console.log(commentsRemovedSec2);

Upvotes: 6

Views: 31404

Answers (1)

evolutionxbox
evolutionxbox

Reputation: 4122

Unfortunately .append() isn't an Array method.

Instead use the Array method .push().

commentsRemovedSec2.push(z)

The push() method adds one or more elements to the end of an array and returns the new length of the array. MDN

Upvotes: 26

Related Questions