Array array operation
Array array operation
Function
Array prototype function provides add, delete, modify, sort, slice, flatten, deep copy and high-order traversal capabilities.
API List
| API | Description |
|---|---|
| Array.len | Returns the number of array elements. |
| Array.to_string | Serialize an array into a JSON string. |
| Array.join | Splice the array elements into a string according to the delimiter. |
| Array.each | Traverse the array and execute callbacks. |
| Array.pop | Delete and return the last element of the array. |
| Array.first | Returns the first element of the array; returns empty for an empty array. |
| Array.last | Returns the last element of the array; returns empty for an empty array. |
| Array.at | Read elements by subscript, and support reading negative numbers from the tail. |
| Array.push | Append one or more elements to the end of the array. |
| Array.shift | Delete and return the first element of the array. |
| Array.unshift | Insert one or more elements into the beginning of the array. |
| Array.insert | Insert one or more elements at the specified index. |
| Array.sort | Sort an array. When no callback is passed, the order is sorted by the element string value; when the callback is passed, the order is determined by the number returned by the callback. |
| Array.reverse | Reverse the order of array elements. |
| Array.slice | Copy the array fragment according to the starting and ending indexes. |
| Array.splice | Removes the specified range from the array and inserts new elements. |
| Array.concat | Consolidate the current array and subsequent parameters. Array parameters are expanded one level, and non-array parameters are appended element by element. |
| Array.contains | Determine whether the array contains the specified value. |
| Array.index_of | Returns the index of the first occurrence of the specified value. |
| Array.last_index_of | Returns the index of the last occurrence of the specified value. |
| Array.find | Returns the first element for which the callback result is true. |
| Array.find_index | Returns the index of the first element for which the callback result is true. |
| Array.find_last | Returns the first element from right to left for which the callback result is true. |
| Array.find_last_index | Returns the first element index from right to left for which the callback result is true. |
| Array.every | Determine whether all elements in the array make the callback result true. |
| Array.some | Determine whether there is an element in the array and make the callback result true. |
| Array.filter | Filter elements so that the callback result is true. |
| Array.map | Traverse the array and form a new array with the callback return value. |
| Array.reduce | Accumulate array elements from left to right. |
| Array.reduce_right | Accumulate array elements from right to left. |
| Array.fill | Fills a range of the array with the specified value. |
| Array.flat | Flatten a nested array by depth. |
| Array.flat_map | First map and then expand the mapping result by depth 1. |
| Array.keys | Returns an array of array subscripts. |
| Array.values | Returns a shallow copy of the array. |
| Array.entries | Returns an array of entries in the form [index, value]. |
| Array.clone | Returns an array deep copy. |
| Array.delete | Delete the element at the specified index. |
| Array.remove_at | Delete and return the element with the specified subscript, supporting negative subscripts. |
| Array.clear | Clear the array and return the original array. |
| Array.is_empty | Determine whether the array is empty. |
| Array.unique | Returns the new array after deduplication in order of first appearance. |
| Array.chunk | Split the array into a two-dimensional array according to the specified size. |
Examples
items = [1, 2, 3] result = items.map(fn(value) { value * 2 }) // Output: [2,4,6] print result
Notes
- push, pop, shift, unshift, insert, sort, reverse, splice, fill, delete, remove_at, clear will modify the original array.