This is the 13th day of my participation in the August More Text Challenge.More challenges in August

An array object uses a single variable name to store a series of values. TypeScript declares arrays in the following syntax:

var array_name[:datatype]; // declare array_name = [val1,val2,valn..] / / initializationCopy the code

Or initialize it directly at declaration time:

Var array_name[:data type] = [val1,val2... valn]Copy the code

Example:

Var nums: Number = [2, 4,6,8];Copy the code

The entire array structure looks like this:

The Array object

We can also create arrays using Array objects. The constructor of an Array object accepts two values:

  • A numeric value representing the size of the array.
  • An initialized arraylist of elements separated by commas.

Code examples:

var arr_names:number[] = new Array(4)  
 
for(var i = 0; i<arr_names.length; i++) { 
        arr_names[i] = i * 2 
        console.log(arr_names[i]) 
}
Copy the code

An array of deconstruction

We can also assign array elements to variables as follows:

Var arr:number[] = [12,13] var[x,y] = arr // Assigns two elements of the array to variables x and y console.log(x) // 12 console.log(y) // 13Copy the code

An array of iteration

We can loop through the elements of the array using the for statement:

var j:any; Var nums:number[] = [1001,1002,1003,1004] for(j in nums) {console.log(nums[j])} var nums:number[] = [1001,1002,1003,1004] for(j in nums) {console.log(nums[j])}Copy the code

Multidimensional array

The elements of one Array can be another Array, thus forming a multi-dimensional Array. The simplest multidimensional array is a two-dimensional array, defined as follows:

var arr_name:datatype[][]=[ [val1,val2,val3],[v1,v2,v3] ]
Copy the code
Var multi: number [] [] = [[1, 2, 3], [23,24,25]], the console, log (multi [0] [0]) to the console. The log (multi [0] [1]) to the console. The log (multi [0] [2]) console.log(multi[1][0]) console.log(multi[1][1]) console.log(multi[1][2])Copy the code

Array methods

  • Concat () : Joins two or more arrays and returns the result.
  • Every () : Checks whether each element of a numeric element matches a condition.
  • Filter () : Detects numeric elements and returns an array of all elements that match the criteria.
  • ForEach () : Each element of the array executes a callback.
  • IndexOf () : Searches for an element in an array and returns its location. If it cannot be found, the value -1 is returned, indicating that this item is not available.
  • Join () : Puts all the elements of an array into a single string.
  • LastIndexOf () : Returns the last position of a specified string value, searched from back to front at the specified position in a string.
  • Map () : Processes each element of an array by specifying a function and returns the processed array.
  • Pop () : Removes the last element of the array and returns the deleted element.
  • Push () : Adds one or more elements to the end of the array and returns the new length.
  • Reduce () : Evaluates an array element to a value (left to right).
  • ReduceRight () : Calculate array elements as a value (from right to left).
  • Reverse () : Reverses the order of the elements in an array.
  • Shift () : Removes and returns the first element of the array.
  • Slice () : picks a portion of an array and returns a new array.
  • Some () : Checks whether any element in the array meets the specified condition.
  • Sort () : Sorts the elements of an array.
  • Splice () : Adds or removes elements from an array.
  • ToString () : Converts an array to a string and returns the result.
  • Unshift () : Adds one or more elements to the beginning of the array and returns the new length.

Code examples:

let arr1 = ['a', 'b']; let arr2 = ['c', 'd']; // concat():concat(... items: ConcatArray<T>[]): T[]; console.log(arr1.concat(arr2)); // [ 'a', 'b', 'c', 'd' ] // every():every(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg? : any): boolean; console.log(arr1.every(x => x === 'a')); // false // some() : some(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg? : any): boolean; console.log(arr2.some(x => x === 'c')); // true // filter() : filter(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg? : any): T[]; console.log(arr1.filter(x => x ! == 'a')); // [ 'b' ] // forEach(): forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg? : any): void; arr1.forEach(x => console.log(x)); // a/nb/map() : map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg? : any): U[]; console.log(arr1.map(x => {console.log(x); return `${x}${x}`})); // a/n ['aa', 'bb'] // indexOf() : indexOf(searchElement: T, fromIndex? : number): number; console.log(arr1.indexOf('a')); // 0 // lastIndexOf() : lastIndexOf(searchElement: T, fromIndex? : number): number; console.log(arr1.lastIndexOf('a', 0)); / / 0 / / pop () : the pop () : T | undefined; console.log(arr1.pop()); // b console.log(arr1); // ['a'] // push() : push(... items: T[]): number; console.log(arr1.push('b')); // 2 console.log(arr1); // ['a', 'b'] // reduce(): //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; console.log(arr1.concat(arr2).reduce((x,y) => x+y)); // abcd //reduceRight(): //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; console.log(arr1.concat(arr2).reduceRight((x,y) => x+y)); // dcba // reverse():reverse(): T[]; console.log(arr1.reverse()); ['b', 'a'] // sort() : sort(compareFn? : (a: T, b: T) => number): this; console.log(arr2.reverse().sort()); // [ 'c', 'd' ] console.log(arr2.sort((a,b) => b < a ? 1:0)); / / / 'c', 'd'] / / shift () : the shift () : T | undefined; console.log(arr1.shift()); // b console.log(arr1); / / / 'a'] / / unshift () : the console. The log (arr1. Unshift (' c ')); // 4 console.log(arr1); ['c', 'a', 'b', 'a'] // slice() : slice(start? : number, end? : number): T[]; The console. The log (arr2. Slice (0, 1)); // ['c'] // splice() : // start: number, deleteCount? : number): T[]; // splice(start: number, deleteCount: number, ... items: T[]): T[]; console.log(arr1); / / / 'a' console. The log (arr1. Splice (2, 0, 'b', 'a')); // [] console.log(arr1); // ['a', 'b', 'a'] //join() : join(separator? : string): string; console.log(arr1.join('-')); // toString(): toString(): string; console.log(arr1.toString()); // a,b,aCopy the code