ES5 Array

来源:互联网 发布:php jsonp callback 编辑:程序博客网 时间:2024/06/05 14:58

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.