Skip to content

Instantly share code, notes, and snippets.

@goodbedford
Created April 2, 2016 13:54
Show Gist options
  • Save goodbedford/12e5d9e49ac7fe6e73d0ec9fba29a45a to your computer and use it in GitHub Desktop.
Save goodbedford/12e5d9e49ac7fe6e73d0ec9fba29a45a to your computer and use it in GitHub Desktop.
// splice( starting-index, delete-count, new-item)
// start is index exact, delete-count is how many elements to remove including start
// new-item is optional and is added at starting-index
// splice is permenant
var dogs = ["Pit Bull", " American Cocker Spaniel", "Beagle", " Belgian Malinois"];
var newDog = "Bloodhound";
var guideDog = "Brussels Griffon"
// take out Beagle
dogs.splice(2,1);
console.log("dogs - beagle:", dogs);
//add newDog after Pit Bull
dogs.splice(1,0,newDog);
console.log("newDog after Pit Bull:", dogs);
// change one out swap dogs
// swap guideDog for Pit Bull
dogs.splice(0,1,guideDog);
console.log("dogs swap guideDog for Pit Bull:", dogs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment