ES5 Array

xiaoxiao2021-02-28  101

Array.prototype.sort

By default sort function will sort the array alphabetic, so if you want to sort the array according to numeric value, pass in a callback function that compare two numbers.

[10, 2, 20, 5].sort(function (a, b) { return a - b; })

Array.prototype.isArray

isArray method is used to check whether the type of a variable is an array.

Array.prototype.filter

The filter(callback) method created a new array with elements that pass the callback function passed in.

var array = [1, 2, 3, 4] var newArray = array.filter(number => { return number > 2; }) console.log(array) // [1, 2, 3, 4] console.log(newArray) // [3, 4]

Array.prototype.forEach

The forEach(callback) method executes a provided function once for each element. The method implements iterator pattern. There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool. Use a plain loop instead.

转载请注明原文地址: https://www.6miu.com/read-46769.html

最新回复(0)