removing array elements in JavaScript

JavaScript array elements can be removed in multiple ways using methods pop, shift, splice, filter.

Array.shift()
Shift is a method which knocks off the first most element of an array and returns it! ie
var number = [0, 1, 2, 3, 4, 5]
number.shift() // returns 0
console.log(number) // [1, 2, 3, 4, 5]

Array.pop()
this method removes the last element off an array and returns the removed element ie
var number = [0, 1, 2, 3, 4, 5]
number.pop() // returns 5
console.log(number) // [0, 1, 2, 3, 4]

Array.splice(start, elements)
start is the index at which removing  starts elements is an integer the number of elements to remove it returns spliced elements
var number = [0, 1, 2, 3, 4, 5]
number.splice(0, 1) // returns 0
start can also be a reference ie of an object.

Comments

Popular posts from this blog

Tar basic commands to store and extract files

Removing bash.sh #hash comments from a string using regEx in JavaScript

FlappingBird Postmortem (JS13K)

How to build a twitter retweet bot using JavaScript

Npm how to install webpack nodejs