Js string methods, such as substr and substring, array methods, such as slice and splice, have similar names and slightly different usages, which make developers always forget their usage in the development process and need to look up information. Now sort it out, I hope to help you remember.

String object

slice

stringObject.slice(start, end)

var a = 'Hello world! ';
var b = a.slice(2);
var c = a.slice(4 -.2 -);
// a: 'Hello world! '
// b: 'llo world! '
// c: 'rl', which can be negative
Copy the code

substr

stringObject.substr(start, length)

var a = 'Hello world! ';
var b = a.substr(0.4);
var c = a.substr(- 5.2);
// a: 'Hello world! '
// b: 'Hell'
// c: 'or', which can be negative
Copy the code

substring

stringObject.substring(start, stop)

var a = 'Hello world! ';
var b = a.substring(0.4);
var c = a.substring(3.2);
var d = a.substring(0.- 1);
// a: 'Hello world! '
// b: 'Hell'
// c: 'l', start is smaller than stop, swap these two parameters
// d: ", with a negative argument, returns an empty string
Copy the code

Slice, substr, and substring are all string slicing methods. They are slightly different and can be used flexibly according to different application scenarios. All three methods generate a new string rather than modifying the original string.

The Array object

concat

arrayObject.concat(arrayX, arrayX, …… , arrayX)

The parameters can be specific values or array objects, or any number of them. Returns a copy of the concatenated array without altering the existing array.

var a = [1.2.3];
var b = a.concat(4.5);
var c = a.concat([4.5]);
// a: [1, 2, 3]
// b: [1, 2, 3, 4, 5]
// c: [1, 2, 3, 4, 5]
Copy the code

pop

arrayObject.pop()

Deletes the last element of the arrayObject, reduces the array length by one, and returns the value of the element it deleted. If the array is already empty, pop() does not change the array and returns undefined. This method changes the original array.

var a = [1.2.3];
var b = a.pop();
// a: [1, 2], modify the original array
// b: 3, returns the value of the deleted element
Copy the code

push

arrayObject.push(newelement1,newelement2,…. ,newelementX)

The parameters are sequentially added to the tail of the arrayObject to modify the arrayObject directly.

var a = [1.2.3];
var b = a.push(4.5);
// a: [1, 2, 3, 4, 5], modify the original array
// b: 5, returns the length of the modified array
Copy the code

shift

arrayObject.shift()

Removes the first element from an array and returns the value of the first element. If the array is empty, the shift() method does nothing and returns undefined. This method changes the original array. Analogy to pop method.

var a = [1.2.3];
var b = a.shift();
// a: [2, 3], modify the original array
// b: 1, returns the value of the deleted element
Copy the code

unshift

arrayObject.unshift(newelement1, newelement2, …. , newelementX)

Adds one or more elements to the beginning of the array and returns the new length. The first argument to the method will be the new element 0 of the array, if there is a second argument, it will be the new element 1, and so on.

var a = [1.2.3];
var b = a.unshift(4.5);
// a: [4, 5, 1, 2, 3], modify the original array
// b: 5, returns the length of the modified array
Copy the code

slice

arrayObject.slice(start, end)

Returns a new array containing elements from the arrayObject from start to end (excluding the element). This method does not modify the original array.

var a = [1.2.3.4.5];
var b = a.slice(2);
// a: [1, 2, 3, 4, 5] does not modify the original array
// b: [3, 4, 5], returns a new array

var c = [1.2.3.4.5];
var d = c.slice(2.- 1);
// c: [1, 2, 3, 4, 5]
// d: [3, 4], returns a new array
Copy the code

splice

arrayObject.splice(index, howmany, item1, ….. , itemX)

Zero or more elements starting at index can be deleted, and those deleted elements can be replaced with one or more values declared in the parameter list. If an element is deleted from an arrayObject, an array containing the deleted element is returned.

var a = [1.2.3.4.5];
var b = a.splice(1.1);
// a: [1, 3, 4, 5], modify the original array
// b: [2], returns a new array

var c = [1.2.3.4.5];
var d = c.splice(- 1.1);
// c: [1, 2, 3, 4], modify the array
// d: [5], returns a new array

var e = [1.2.3.4.5];
var f = e.splice(1.1.6.7);
// e: [1, 6, 7, 3, 4, 5], modify the original array
// f: [2], returns a new array

var g = [1.2.3.4.5];
var h = g.splice(1.0.8);
// g: [1, 8, 2, 3, 4, 5]
// h: [], returns an empty array without deleting the value
Copy the code

sort

arrayObject.sort(sortBy)

With no arguments, the elements in the array are sorted alphabetically. Returns a value greater than 0 if the values of prev and next are swapped when the argument is a comparison function.

var a = [1.10.8.6.9];
var b = a.sort(function (prev, next) {
  return prev - next;
});
// a: [1, 6, 8, 9, 10], modify the original array
// b: [1, 6, 8, 9, 10], returns the modified array
Copy the code

reverse

arrayObject.reverse()

Used to reverse the order of elements in an array. It changes the original array.

var a = [1.2.3];
var b = a.reverse();
// a: [3, 2, 1], modify the original array
// b: [3, 2, 1], returns the modified array
Copy the code

map

arrayObject.map(function(currentValue, index, arrayObject) {})

Returns a new array by processing each item in the array.

var a = [1.2.3];
var b = a.map((curVal) = > curVal * 2);
// a: [1, 2, 3], do not modify the original array
// b: [2, 4, 6], returns a new array
Copy the code

forEach

arrayObject.forEach(function(currentValue, index, arrayObject) {})

Each element of the array performs the provided function once. In general, the original array is not modified, but it can also be modified through processing functions. This method is flexible and can be compared to… Of.

var a = [1.2.3];
var sum = 0;
var b = a.forEach((curVal) = > sum += curVal);
// a: [1, 2, 3], do not modify the original array
// b: undefined, forEach does not return a value
// sum: 6
Copy the code

find

arrayObject.find(function(currentValue, index, arrayObject) {})

Returns the first element in the array that meets the test condition (true). If no such element exists, return undefined. FindIndex is similar, except that it returns the index of the first element that meets the test criteria.

var a = [1.2.3];
var b = a.find((curVal) = > curVal === 1);
var c = a.find((curVal) = > curVal === 4);
// a: [1, 2, 3], do not modify the original array
// b: 1
// c: undefined
Copy the code

filter

arrayObject.filter(function(currentValue, index, arrayObject) {})

Returns an array of all elements in the array that meet the test criteria (returning true). If no such element exists, return [].

var a = [1.2.3];
var b = a.filter((curVal) = > curVal > 1);
var c = a.filter((curVal) = > curVal > 3);
// a: [1, 2, 3], do not modify the original array
// b: [2, 3]
// c: []
Copy the code

reduce

arrayObject.filter(function(previousValue, currentValue, currentIndex, arrayObject) {}, initialValue)

An accumulator is received, and each value in the array (from left to right) is reduced to a single value.

If there is no initialValue parameter,reduce executes the callback function from index 1 and skips the first index. If there is an initialValue parameter, reduce will perform a callback starting at index 0. If the array is empty and there is no initialValue parameter, TypeError will be raised. If the array has only one element and there is no initialValue initialValue, or if there is an initialValue but the array is empty, the unique value is returned without calling the callback function.

var a = [1.2.3];
var b = a.reduce((prevResult, curItem) = > prevResult + curItem);
// a: [1, 2, 3], do not modify the original array
// b: 6
Copy the code

None of the other methods modify the Array except the pop, push, Shift, unshift, splice, sort, and reverse methods.