Method to add items to an array

Var ary = [1, 2, 3]

  • Append the array to the end using the array’s length property
    • ary[ary.length]=4; Add the number 4 to the end of the array
  • Push method: increments the end of an array
    • The return value of push is the changed array length
var ary = [1, 2, 3]; Ary. Push ("push in ",2,3,4); console.log(ary); [1, 2, 3, "push in ", 2, 3, 4] length: 7Copy the code
  • Unshift increments the beginning of the array, passing as many arguments as possible
    • Changes the index of the existing array
    • The return value of unshift is the changed length of the array
Ary. Unshift (" I am added by unshif ",8) increments at the front of the arrayCopy the code

Method to delete items from an array

  • Pop deletes the last item in the array
    • The return value of pop is the deleted item
    • Changes the index of the array
var ary1 = [4, 5, 6]; var res=ary1.pop(); console.log(res); The output is 6Copy the code
  • Shift deletes the first item in the array
    • Changes the index of the existing item in the array
    • The return value is the deleted item
var ary1 = [4, 5, 6]; console.log(ary1); console.log(ary1.shift()); The result is 4Copy the code
  • Delete Deletes key-value pairs or items in an array
    • Does not change the index of the original item in the array
delete ary1[1]; [4, empty, 6] //length is still 3;Copy the code
  • Length == Deletes the last item in the array
    • Here we use the length operation to change the array
    • The output depends on the position of —

Splice can add, delete, or change

The first parameter indicates which index to start from, the second parameter indicates how many items to delete, and the third parameter indicates which items to add

  • Splice (n,0,x); splice(n,0,x); splice(n,0,x)
  • Splice (n,m) deletes m elements from index n and stores the deleted contents in a new array
  • Splice (n, m, x) drops m elements from index n and places the added elements at index n
  • The return value is an array of deleted items
  • Ary2. splice(0,0,” increment “); It does not delete from index 0 and has a third parameter which is added to the first item
  • Ary2. splice(0,1,” modified item “); Removes an element from the position at index 0 and adds a third element to that position

Verifies that an item is in an array

  • Var ary2 = [” “,” “,” “];

IndexOf checks whether an item is in the array. If the value returned is negative one, it is not in the array. If it is in an array, it returns the corresponding index

Var res2 = ary2.indexof ("小 小 "); Var res2 = ary2.indexof (" old "); var res2 = ary2.indexof (" old "); / / = > 0Copy the code

LastIndexOf goes back to front

Var res3 = ary2. LastIndexOf (" chan "); //=>3 console.log(res3); The last old index is 3, so the last old index is 3Copy the code

Includes verifies that the item is in the array

Yes returns true, no returns false

Var res4 = ary2.includes(” Chen “)

console.log(res4);

Methods that do not change the original array

  • Concat Concatenation of arrays (or values) into a single array, leaving the original array unchanged and returning a new concatenated array
    • Var ary1 = [4 and 6, “chan”];
    • If the argument is an array, one layer of the array is stripped []
    • Multiple parameters can be passed, all concatenated together
    • Var res=ary1. Concat ([” macro “,” chapter “]);
    • It’s a clone of an array without passing any arguments, right
      • var res =ary1.concat(); The output is [4,5,6,” Chen “]
  • Slice (n, m) copies from index n to index M (excluding index M) and returns the found contents as a new array, unchanged from the original array.
Var ary = [6]; Var res = ary. Slice (0, 4); console.log(ary); // Output the original array console.log(res); // print [1,2,3,4] from index 0 to index 4, excluding index 4Copy the code
  • slice(-3,-2)
    • From index (-3+length) to (-2+length) excluding -2+length
Var ary = [6]; var res =ary.slice(-3,-2); console.log(ary); // Output the array console.log(res); // Output 4, because 6-3=3, 6-2=4 (3, 4)Copy the code
  • Cloned array
    • Var res =ary. Slice (0), which can be understood as finding each item in the original array and returning the new array, to clone the array. The new array and the original array are two different heaps, but the storage content is the same
    • console.log(ary);
    • console.log(res);
    • If the second argument is larger than length, go straight to the end

Convert an array to a string:

  • toString(); Each item in the array is “comma-separated” and concatenated into a corresponding string, leaving the original array unchanged
Let arr =,20,30,40,50,60,70 [10] the console. The log (arr. ToString ()); // output result is '10,20,30,40,50,60,70'Copy the code
  • Join ([char]); Specifying delimiters
    • Join concatenates each item in an array with characters and returns a string
Var res =arr.join("+"); var res =arr.join("+"); // var res =arr.join(); ToString () console.log(res); toString () console.log(res);Copy the code
  • Eval allows strings to be executed as expressions
    • If the above string can be executed as a JS expression, it represents the sum of each item in the array
Eval. log(eval(res)) to turn strings into expressionsCopy the code

Sort an array

  • Reverse sorts each item in the array upside down
  • Sort an array
  • If no parameter is passed, only the first digit is sorted
  • The numbers are arranged in ascending order
  • The letters are in coded order
console.log(res); var res2=ary.sort((a,b)=> { console.log(a,b); return a-b; // return b-a; }) console.log(ary,res2); var arrobj=[ {name:"q",age:12}, {name:"qw",age:22}, {name:"er",age:11}, {name:"sdf",age:1} ]; arrobj.sort((a,b)=>{ return a.age-b.age; }) console.log(arrobj);Copy the code

String methods

  • indexOf
  • lastindexOf
  • includes
  • Substr (n,m) intercepts m characters starting from index n. Negative numbers are not supported
  • Slice (n,m) intercepts from index n to index M, excluding m, and supports negative numbers
  • Substring (n,m) intercepts from index n to index m, excluding m
Var str2 = 'family countdown '; var res = str2.substr(0, 3); var res1 = str2.substring(2, 5); var res2 = str2.slice(2, 5); console.log(res1,res,res2)Copy the code

The character that gets the string-related index

  • CharAt gets the character associated with the string index
  • CharCodeAt retrieves the characters in the string index and converts the characters to the numbers in the ASCII table
  • String. FromCharCode converts digits in an ASCII table into characters
Var res = STR. CharAt (0); var res1 =str.charCodeAt(0); console.log(res1); console.log(String.fromCharCode(19968)); console.log(str[0]);Copy the code

Uppercase

  • ToUpperCase () converts all letters toUpperCase
  • Change all letters toLowerCase toLowerCase()
var abc='abcdefg';
        console.log(abc.toUpperCase());
        console.log(abc.toLowerCase());
Copy the code

Implement capitalization

  • 1. Take the first letter, then uppercase it
  • 2. Get the rest of the characters except the initial letter, and splice them together
Var res = ABC. Substring (0, 1). The toUpperCase () + the substring (1); console.log(res);Copy the code

Specifies the character that splits the string into each item in the array

\

\

Var STR ='1+2+3'; // var res =str.split("+"); // [1,2,3] // console.log(res); var str2 ='https://www.baidu.com/s?wd=JS&rsv_spt=1&a=1&b=2' var res4 = str2.split("?"); var ary =res4[1].split("&"); // console.log(ary); var obj={}; Ary. ForEach ((item,index)=>{// item is each item in the array // index is the index of the item // console.log(item,index); var inner =item.split('='); // var shuxingming=inner[0]; // var shuxingzhi=inner[1] // obj[shuxingming]=shuxingzhi; obj[inner[0]]=inner[1]; console.log(obj); })Copy the code

A common iteration method in an array

  • Foreach ([function])
    • Iterating over each item in the array (how many items in the array, how many times the function will be executed successively, each time the function is executed, the current iterating item and corresponding index can be obtained in the function)
Arr. ForEach (function(item,index){// This function is looped five times (there are five items in the array) //item: current loop over the contents of this item //index: current index console.log(item,index)})Copy the code

Array summation

  • join
Let arr =,20,30,40,50 [10] the console. The log (eval (arr. Join (' + '))); // If there is an invalid number in the array, the result is NaNCopy the code
  • Through the for loop
let total=0; for(let i=0; i<arr.length; i++){ total+=arr[i]; } console.log(total)Copy the code
  • The forEach method
let total=0;
arr.forEach(function(item.index){
item =Number(item);
if(isNaN(item)===false){
total+=item;

}
})
console.log(total);
Copy the code
  • Map method :forEach does not support return value, but map can support return value on the basis of forEach, the value of each item in the original array is replaced as the new value, and finally stored in a new array, but the original array is unchanged
Let arr =,20,30,40,50 [10]; Let reault=arr.map(function(item,index){// this function is looped five times (there are five items in the array) //item: The index of the current item // returns whatever the current item in the array is. }) console.log(result);Copy the code