Posts

Showing posts with the label Javascript Array Methods

Array Methods in JavaScript for adding and removing items from the beginning and end of a list

  Methods that use arrays Arrays have a plethora of methods. They are divided into groups in this chapter to make things easier. Items can be added or removed. We already have methods for adding and removing items from the beginning and end of a list: arr.push(...items) – pushes things to the end of the list. arr.pop() – pulls a single item from the end of the array. arr.shift() – extracts a single item from the start. arr.unshift(...things) – inserts items at the start. const arr = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑']; Array Methods in JavaScript 1. map( ) This method returns a new array with the results of running a function on each member of the array. const newArr = numbers.map; const numbers = [4, 9, 16, 25]; (Math.sqrt) 2. filter( ) This method produces a new array containing only elements that satisfy the criteria in the function. This function is used to sort the items of an array in either ascending or descending or...