Wednesday 25 January 2017

javascript - How would you remove all objects from an array?




So, how would you remove all objects from an array.. My current code is:



            var array = [""];
function add() {
var input = document.getElementById("enterInput").value;
array.push(input);


var arrayOut = document.getElementById("output").innerHTML = array;
}

function clearit() {
var length = array.length;
array.splice(0, length, length);
array.push("");
}



Thanks, Noah..
(and yes, I have checked the console and no errors are returned.)


Answer



Just use



function clearit() {
array = []; //or array = [""];
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...