This is the 10th day of my participation in the August More Text Challenge

pop()

  • Use: To remove and return the last element of an array.
  • Grammar:arr.pop()
  • Returns when the array is emptyundefined
  • Pop () is reference: Array. The prototype.
  • Example:
var books = [A Dream of Red Mansions.'Water Margin'.Romance of The Three Kingdoms.Journey to the West]
console.log(books.pop())  / / journey to the west
Copy the code
  • Memory:pop()It’s shorter, so it’sdelete.push()It’s longer, so it’sadd. In the same way,shift()Shorter, toodeleteAnd theunshift()It’s longer, so it’sadd.

push()

  • Purpose: To add one or more elements to an array and return the new length of the array.
  • Grammar:arr.push(element1, ... , elementN)
  • Reference: Array. Prototype. Push ()
  • Example:
var books = [A Dream of Red Mansions.'Water Margin'.Romance of The Three Kingdoms.Journey to the West]
var len = books.push('Three Hundred Tang Poems'.'As the palm of the Buddha')
console.log(len)  / / 6
console.log(books)  / / Array (6) [" a dream of red mansions ", "water margin", "three kingdoms", "journey to the west", "started the first", "such as return palm"]
Copy the code

shift()

  • Use: To remove and return the first element of an array.
  • Grammar:arr.shift()
  • Reference: Array. The prototype. The shift ()
  • Example:
var books = [A Dream of Red Mansions.'Water Margin'.Romance of The Three Kingdoms.Journey to the West]
books.shift()  // 'Dream of Red Mansions'
Copy the code

unshift()

  • Purpose: To add one or more elements to the beginning of an array and return the new length of the array.
  • Grammar:arr.unshift(element1, ... , elementN)
  • Reference: Array. The prototype. The unshift ()
  • Example:
var books = [A Dream of Red Mansions.'Water Margin'.Romance of The Three Kingdoms.Journey to the West]
var len = books.unshift('Three Hundred Tang Poems'.'As the palm of the Buddha')
console.log(len)  / / 6
console.log(books)  / / Array (6) [" started the first ", "such as return palm", "a dream of red mansions", "water margin", "three kingdoms", "journey to the west"]
Copy the code

reverse()

  • Purpose: Reverse the original array and return
  • Will change the original array, similarPythonIn the list ofreverse()methods
  • Grammar:arr.reverse()
  • Reference: Array. The prototype. The reverse ()
  • Example:
let nums = [1.2.3]
nums.reverse()
console.log(nums)  // Array(3) [ 3, 2, 1 ]
Copy the code

sort()

  • Purpose: Sort an array and return the sorted array
  • Grammar:arr.sort([compareFunction])
  • compareFunction(firstEl, secondEl)It’s a comparison function, like thatPython 2The comparison function for the list in.
  • Note: The default collation is to convert elements to strings and then to theirunicodeCode to sort, so[11] 2,The result of sorting is[11, 2), is not the expected ascending sort result. If you want to implement ascending sort, you must customize the comparison function, you can use the arrow function.
  • Reference: Array. The prototype. The sort ()
  • Example:
let letters = ['b'.'a'.'c']
letters.sort()
console.log(letters)  // Array(3) [ "a", "b", "c" ]

// Use the comparison function to sort in ascending order
let nums = [2.11]
nums.sort()
console.log(nums)  // Array [ 11, 2 ]
nums.sort((a, b) = > a - b)
console.log(nums)  // Array [ 2, 11 ]
Copy the code

splice()

  • Purpose: To delete and replace an element in an array and return the deleted contents as an array. This method changes the array.
  • Grammar:array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
  • startIs the index to start dropping,deleteCountHow many do I delete? In the backitemIs the element to be added after deletion
  • Reference: Array. The prototype. The splice ()
  • Example:
// From index 1, delete 1 element and add the following elements
let arr = ['我'.'and'.'you']
arr.splice(1.1.'And')  // Return values: Array [" and "]
console.log(arr)  // Array(3) [" I ", "And", "you"]
Copy the code

concat()

  • Purpose: Merge two or more arrays, return a new array. This method does not change the original array.
  • Grammar:var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
  • If called without arguments, it gets a shallow copy of the original array.
  • Reference: Array. The prototype. The concat ()
  • Example:
let arr1 = [1.2.3]
let arr2 = [4.5]
let arr3 = [6]
// Note: arr1 is concatenated one more time because arr1 is in the parameter
let arr = arr1.concat(arr1, arr2, arr3)
console.log(arr)  // Array(9) [ 1, 2, 3, 1, 2, 3, 4, 5, 6 ]
Copy the code

includes()

  • Purpose: Check whether the current array contains a given element, return a Boolean value.
  • Grammar:arr.includes(valueToFind[, fromIndex])
  • Reference: Array. Prototype. Includes ()
  • Example:
let people = ['Joe'.'bill']
console.log(people.includes('Cathy'))  // false
Copy the code

join()

  • Purpose: Concatenate all elements of an array using a delimiter to return a concatenated string
  • Grammar:arr.join([separator])
  • The default separator is.. This method is similar in functionPythonOf the string inseparator.join(iterable).
  • Reference: Array. The prototype. The join ()
  • Example:
let nums = [1.2.3]
nums.join(The '*')  / / "1 * 2 * 3"
Copy the code

slice()

  • Purpose: Return a shallow copy of the open range of the array
  • Grammar:arr.slice([begin[, end]])
  • A bit likePythonSection syntax
  • Reference: Array. The prototype. The slice ()
  • Example:
let nums = [1.2.3.4.5]
let nums2 = nums.slice(2.4)
console.log(nums2)  // Array [ 3, 4 ]
Copy the code

indexOf()

  • Purpose: Returns the index of the first occurrence of a given element in the current array, or if it cannot be found- 1
  • Grammar:arr.indexOf(searchElement[, fromIndex])
  • Reference: Array. Prototype. IndexOf ()
  • Example:
let arr = [1.3.5.7]
console.log(arr.indexOf(3))  / / 1
Copy the code

lastIndexOf()

  • Purpose: To return the index of the first occurrence of a given element in the current array from back to front, or if it cannot be found- 1
  • Grammar:arr.lastIndexOf(searchElement[, fromIndex])
  • fromIndexIs the initial search position, default if not writtenarr.length - 1
  • Reference: Array. The prototype. The lastIndexOf ()
  • Example:
let arr = [1.3.5.7]
console.log(arr.lastIndexOf(3))  / / 1
Copy the code

filter()

  • Purpose: Returns a new array filtered using the specified function
  • Grammar:var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
  • callbackIs a callback function for filtering,elementIs the element currently passed in,indexIs the index of the element in the array,arrayIs the original array,thisArgIs used to specify thecallbackIn thethis
  • Reference: Array. The prototype. The filter ()
  • Example:
let nums = [1.2.3.4.5.6.7]

// Filter all even numbers
let even = nums.filter(el= >! (el %2))
console.log(even)  // Array(3) [ 2, 4, 6 ]
Copy the code

map()

  • Purpose: To return a new array processed with the given function
  • Grammar:
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
 // Return element for new_array 
}[, thisArg])
Copy the code
  • callbackParameter meaning andfilter()The callback function in the method is similar
  • Reference: Array. The prototype. The map ()
  • Example:
let nums = [1.2.3]

// Multiply all elements of the array by 2
let two = nums.map(el= > 2 * el)
console.log(two)
// Array(3) [ 2, 4, 6 ]
Copy the code

reduce()

  • Purpose: Simplify an array, return a single value.
  • Grammar:arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
  • callbackEach value in the array is called once
  • accumulatorIs the return value from the last call to the callback function, or isinitialValue
  • currentValueIs the element being processed
  • indexIs the index of the element being processed. If providedinitialValue, the start index number is0Otherwise from the index1The start.
  • arrayFor the callreduce()An array of
  • initialValueIs the first callcallbackaccumulatorIf not provided, the first element of the array is used. In the absence ofinitialValueIs called on an empty arrayreduceWill be an error.
  • Reference: Array. The prototype. The reduce ()
  • Example:
let nums = [1.2.3.4]

/ / accumulation
let sum = nums.reduce((pre, cur) = > pre + cur)
console.log(sum)  / / 10

/ / multiplicative
let product = nums.reduce((pre, cur) = > pre * cur)
console.log(product)  / / 24
Copy the code