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

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript