// standard Array
someArray = [];
// JSON = Javascript Object Notation
userData = {
"FirstName": "Jonathon",
"Age": 37,
"Phone numbers": [049320495, 0485830284, 027847382]
};
$("#addItem").click(function() {
//do something
someArray.push(someArray.length+1);
console.log(someArray);
printArray();
});
$("#removeItem").click(function() {
//do something
//someArray.pop();
//someArray.shift();
//console.log(someArray);
// Get the number to remove
numToRemove = $("#numberToRemove").val();
// Parse as an INTEGER
numToRemove = parseInt(numToRemove);
//Get the index of the item to remove
var index = someArray.indexOf(numToRemove);
//Sorry - needed to make sure it wasn't -1
if (index != -1) {
//Remove the item
someArray.splice(index, 1);
console.log(someArray);
}else {
window.alert("That item " + numToRemove + " is not in the array!");
}
//Print the array
printArray();
clearInputValue();
//DEBUG
//console.log(someArray);
});
function clearInputValue () {
$("#numberToRemove").val("");
}
$("#clearAll").click(function() {
//do something
let theLengthOfArray = someArray.length;
for(var i = 0; i < theLengthOfArray; i++){
someArray.pop();
printArray();
}
});
function printArray() {
$("#arrayOutput").text("");
// for some reason the var item wasn't working
for(i = 0; i < someArray.length; i++) {
console.log(someArray[i]);
$("#arrayOutput").append(someArray[i]);
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.