Array数组操作小计(es5)

192 阅读8分钟

前言

所有函数使用说明都是今天看源码里面包含的,做一个记录和分享与各位共勉,加上自己的理解,有什么问题欢迎指出来~

unshift

在数组第一个值之前添加 会改变原数组


list 可以当做 let list=new Array[];
list.unshift();

push

在最后添加 会改变原数组

list.push() 

shift

删除数组第一个元素的值,并返回 被删除对象的值

list.shift()

pop

数组中删除最后一个值,并且返回删除的值

list.pop()

splice

删除数组里面的对象

  • 第一个参数为起始位置
  • 第二个参数为删除数量 会包含起始位置开始删除
  • 第三个参数传入一个数组对象 替换刚刚删除的数组对象 可选参数
list.splice(0,1)

toString

数组转换成字符串

list.toString()

toLocaleString

数组转换字符串,每个对象会默认加一个逗号

list.toLocaleString() 

concat

组合两个或者多个数组合并 并返回一个新数组

list.concat() 

join

传出字符串参数 作为分隔数组每个对象的分隔符,不传默认为逗号

list.join()

reverse

反转数组

list.reverse()

slice

  • 第一个参数设置部分开始的头部索引
  • 第二个参数设置尾部部分的索引,返回的数组不包含尾部索引的值 返回部分数组
list.slice()

sort

排序数组 默认升序排序 (a,b)=>{return a-b} 第一个值大于第二个值 则升序排序,反之降序 通俗点就是 a-b = 升 ,b-a = 降

list.sort() 

indexOf

返回指定参数 在数组中第一次出现的索引

  • 第一个参数 制定查找的值
  • 第二个参数 开始搜索的索引,忽略则从第一个索引开始查找
list.indexOf()

lastIndexOf

返回指定参数 在数组中最后一次出现的索引

  • 第一个参数 指定查找的值
  • 第二个参数 开始搜索的索引,忽略则从最后一个索引开始查找
list.lastIndexOf()

every

测试遍历函数 用于测试数组中的所有值是否都满足判断条件 返回一个boolean (item)=>{item > 30} [2,34,30] = false

list.every()

some

测试遍历函数 用于测试数组中的所有值是否有一个满足判断条件 返回一个boolean (item)=>{item > 30 } [2,34,30] = true

list.some()

forEach

遍历数组对象函数 给每一个数组元素指定一个函数 无返回值

list.forEach()

map

对数组的每个元素调用已定义的回调函数,并返回包含结果的数组。 会返回一个新数组

list.map()

filter

对数组每个元素进行判断过滤 并返回包含结果的数组

list.filter()

reduce

为数组中的所有元素调用指定的回调函数。回调函数的返回值是累积的结果,并在下次调用回调函数时作为参数提供。 最后一个参数 为 初始值 置之后将从初始值开始累计 从左到右累计

list.reduce()

reduceRight

功能和reduce 一样,但是累计顺序和reduce函数是相反的 从右向左累计

list.reduceRight()

源码

来自lib.es5.ts

interface Array<T> {
   /**
    * Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
    */
   length: number;
   /**
    * Returns a string representation of an array.
    */
   toString(): string;
   /**
    * Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
    */
   toLocaleString(): string;
   /**
    * Removes the last element from an array and returns it.
    */
   pop(): T | undefined;
   /**
    * Appends new elements to an array, and returns the new length of the array.
    * @param items New elements of the Array.
    */
   push(...items: T[]): number;
   /**
    * Combines two or more arrays.
    * @param items Additional items to add to the end of array1.
    */
   concat(...items: ConcatArray<T>[]): T[];
   /**
    * Combines two or more arrays.
    * @param items Additional items to add to the end of array1.
    */
   concat(...items: (T | ConcatArray<T>)[]): T[];
   /**
    * Adds all the elements of an array separated by the specified separator string.
    * @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
    */
   join(separator?: string): string;
   /**
    * Reverses the elements in an Array.
    */
   reverse(): T[];
   /**
    * Removes the first element from an array and returns it.
    */
   shift(): T | undefined;
   /**
    * Returns a section of an array.
    * @param start The beginning of the specified portion of the array.
    * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
    */
   slice(start?: number, end?: number): T[];
   /**
    * Sorts an array.
    * @param compareFn Function used to determine the order of the elements. It is expected to return
    * a negative value if first argument is less than second argument, zero if they're equal and a positive
    * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
    * ```ts
    * [11,2,22,1].sort((a, b) => a - b)
    * ```
    */
   sort(compareFn?: (a: T, b: T) => number): this;
   /**
    * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
    * @param start The zero-based location in the array from which to start removing elements.
    * @param deleteCount The number of elements to remove.
    */
   splice(start: number, deleteCount?: number): T[];
   /**
    * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
    * @param start The zero-based location in the array from which to start removing elements.
    * @param deleteCount The number of elements to remove.
    * @param items Elements to insert into the array in place of the deleted elements.
    */
   splice(start: number, deleteCount: number, ...items: T[]): T[];
   /**
    * Inserts new elements at the start of an array.
    * @param items  Elements to insert at the start of the Array.
    */
   unshift(...items: T[]): number;
   /**
    * Returns the index of the first occurrence of a value in an array.
    * @param searchElement The value to locate in the array.
    * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
    */
   indexOf(searchElement: T, fromIndex?: number): number;
   /**
    * Returns the index of the last occurrence of a specified value in an array.
    * @param searchElement The value to locate in the array.
    * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
    */
   lastIndexOf(searchElement: T, fromIndex?: number): number;
   /**
    * Determines whether all the members of an array satisfy the specified test.
    * @param callbackfn A function that accepts up to three arguments. The every method calls
    * the callbackfn function for each element in the array until the callbackfn returns a value
    * which is coercible to the Boolean value false, or until the end of the array.
    * @param thisArg An object to which the this keyword can refer in the callbackfn function.
    * If thisArg is omitted, undefined is used as the this value.
    */
   every(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
   /**
    * Determines whether the specified callback function returns true for any element of an array.
    * @param callbackfn A function that accepts up to three arguments. The some method calls
    * the callbackfn function for each element in the array until the callbackfn returns a value
    * which is coercible to the Boolean value true, or until the end of the array.
    * @param thisArg An object to which the this keyword can refer in the callbackfn function.
    * If thisArg is omitted, undefined is used as the this value.
    */
   some(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
   /**
    * Performs the specified action for each element in an array.
    * @param callbackfn  A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
    * @param thisArg  An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
    */
   forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
   /**
    * Calls a defined callback function on each element of an array, and returns an array that contains the results.
    * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
    * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
    */
   map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
   /**
    * Returns the elements of an array that meet the condition specified in a callback function.
    * @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
    * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
    */
   filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
   /**
    * Returns the elements of an array that meet the condition specified in a callback function.
    * @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
    * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
    */
   filter(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
   /**
    * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
    * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
    * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
    */
   reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
   reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
   /**
    * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
    * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
    * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
    */
   reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
   /**
    * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
    * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
    * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
    */
   reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
   reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
   /**
    * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
    * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
    * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
    */
   reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;

   [n: number]: T;
}