Method Integration of Arrays and Strings

Posted by Chris-the dude on Sat, 24 Aug 2019 05:49:21 +0200

Arrays and strings in js are a bit similar, not essentially, but when traversing and getting them.From an identity point of view, you can see that one is an array and the other is a string; however, when traversal is used, the two are inadvertently confused, resulting in an incorrect method.At the same time, there are several methods that are the same, but note the semantics and that some methods do not affect the original array.Here are some of the ways I've organized arrays and strings that do not guarantee integrity.

Array method

arr.push() adds one or more elements to the end of the array and returns the new length.Will affect the original array

1 var a = [1,2];
2 a.push(3);
3 console.log(a); // [1,2,3]
4 console.log(a.push(8));  //4

arr.unshift() adds an element at the beginning of the array and returns its value.Will affect the original array

1 var a = [1,2,3,"hello"];
2 console.log(a.unshift("world")); // world
3 console.log(a); // ["world",1,2,3,"hello"]

arr.pop() is used to pop up the last element of the array and return the value of that element.Will affect the original array.(equivalent to deleting this element from the array)

1 var a = [1,2];
2 console.log(a.pop()); // 2
3 console.log(a); // [1]

arr.shift() removes the first element of the array and returns the value of the first element.Will affect the original array.(Delete action)

var a = [1,2];
console.log(a.shift()); // 1
console.log(a); // [2]

arr.concat() is used to join two or more arrays and return a new array.Does not affect the original array.

1 var arr = [1,2,3,4];
2 alert(arr.concat(1,2,3,[1,2,3],5).length); //11
3 arr.concat(1,2,4,[1,2,3],5);
4 alert(arr.length)    //4

arr.join() receives a parameter as a delimiter and returns it as a string.Does not affect the original array.It accepts either 0 or 1 parameter, and only the first is valid if more than one parameter is passed.If the parameter is none, it defaults to','

1 [1,2,3,4].join('0').split('')    //["1", "0", "2", "0", "3", "0", "4"]
2 var arr = [1,2,3,4]; arr.join("&");
3 console.log(arr)    //[1,2,3,4]

 

arr.slice(n,m): Used to cut and create a new array without affecting the original array.

  • 1 parameter: intercept from start to end
  • 2 parameters: intercept from start position to end position [a, b) exclude end position itself
  • The end position can also be a negative number (actually calculated as: this negative number + array length) The end position is less than the start position and returns an empty array []
1 var a = [1, 2, 3];
2 console.log(a.slice(1));      // [2, 3]
3 console.log(a.slice(1, 2));      // [2]
4 console.log(a); // [1, 2, 3] The original array has not been changed
5 console.log(a.slice(1, -1));     // (-1 + 3) = 2 > a.slice(1, 2) > [2]
6 console.log(a.slice(1, -100));     // []

arr.splice(n,m,data) is used to intercept data, insert data, and delete data.The operation is the data itself, so it affects the original array.

  • [Beginning position]: When there is only one parameter: delete all remaining data from the beginning of the array and return the deleted data.
  • [Start position, number of truncations] (Note that this does not mean the same as slice's second parameter, which is length): start with N and end with m, delete [n, n+m] elements
  • [Start position, number of truncations, inserted data]: (n, m, data): n as the starting point, m as the length, delete [n, n+ m] elements, and fill data to the deleted location
1 var array = [15, 5, 10, 2, 0];
2 console.log(array.splice(1, 3,1,2,1));  //5,10,2
3 console.log(array);     //15, 1, 2, 1, 0
4 console.log(array.splice(1, 3));  //5,10,2
5 console.log(array);     // [15,0]
6 console.log(array.splice(1));     // 0
7 console.log(array);     //15

arr.reverse() reverses the elements of the array, affecting the original array.

1 var a = [1,2,3,5];
2 console.log(a.reverse());  // [5,3,2,1]

arr.sort(fn) sort method, parameter: fn(a,b): compare functions, sort in ascending order according to the alphabet ASCII when there are no parameters

1 var a = [1, 2, 3, 5, 10, 25];
2 console.log(a.sort());     // [1, 10, 2, 25, 3, 5]

sort() invokes the transition method toString() on each subitem by default before making a judgment.At this time, it is actually based on the size of ASCII code.So "15" < "5".Compare the first and then the second.Some processing is required to sort as we expect.

1 var array = [15, 5, 10, 2, 0];
2 array.sort(function(a,b){
3         return a-b;
4 })
5 console.log(array);  //[0, 2, 5, 10, 15]

The following are the new methods for ES5 (they will not affect the original array)

arr.indexOf() returns the position of the first element found in the array, or -1 if it does not exist

1     var array = [15, 5, 10, 2, 0];
2     console.log(array.indexOf(1));       //-1
3     console.log(array.indexOf(15));     //0

arr.forEach(): Runs the given function on each item in the array. This method does not return a value (traverse the array, get all elements of the array) The parameter is of type function, and by default there is a parameter (traverse the contents of the array, corresponding array index, array itself)

1     var array = [15, 5, 10, 2, 0];
2     array.forEach(function (value, index, array) {
3         console.log(array[index]); 
4     });

arr.map(): Runs the given function on each item in the array, returning the array of results from each call.

1     var array = [15, 5, 10, 2, 0];
2     var num = array.map(function (value, index, array) {
3         return value * 3;
4     });
5     console.log(array, num);      //[15, 5, 10, 2, 0] ,[45, 15, 30, 6, 0]

arr.filter(): Runs the given function on each item in the array, returning an array (filter) that returns the items of true.

1     var array = [15, 5, 10, 2, 0];
2     var num = array.filter(function (value, index, array) {
3         return value >0;
4     });
5     console.log(array, num);         //[15, 5, 10, 2, 0] ,[15, 5, 10, 2]

Two methods of merging

  • arr.reduce(function(pre, next, index, array) {} from left to right
  • arr.reduceRight(function(pre, next, index, array) {} from right to left

some: Run the given function on each item in the array, and if the function returns true on any item, it returns true, otherwise false

every: Runs the given function on each item in the array, returns true if the function returns true for all items, otherwise false

That's all the array methods I know, ES6 hasn't been involved yet, follow up!!

String Method

indexOf(data,start) is used to return the position of a specified character or string in an array or string; the subscript for the position of the current query character, if none, returns -1, and the start indicates where the query begins.

1     var str="hello world!"
2     console.log(str.indexOf("h"));    //0
3     console.log(str.indexOf("i"));    //-1

str.lastIndexOf() determines the index on which a character last appears in a string, if it contains an index that returns it, if it does not contain a return-1.

1     var str="hello world!"
2     console.log(str.lastIndexOf("d"));    // 10
3     console.log(str.lastIndexOf("i"));    //-1

str.charAt() Returns the character at the specified position

1     var str="hello world!"
2     console.log(str.charAt(1));    //e
str.substr(n,m) Begins with index n, intercepts m characters, returns the intercepted characters, and does not change the original string
1     var str="hello world!"
2     console.log(str.substr(1));    //ello world!
3     console.log(str.substr(1,3))   //ell

str.substring(n,m) returns a string from the specified position n to the end position (excluding) m, or from the start to the end if no end position is specified

1     var str="hello world!"
2     console.log(str.substring(1));    //ello world!
3     console.log(str.substring(1,3))   //el

str.slice(n,m) is the same as substring and requires attention to be similar to the method slice() in the array

1     var str="hello world!"
2     console.log(str.slice(1));    //ello world!
3     console.log(str.slice(1,3))   //el

str.split('-') Returns an array by splitting a string with a specified character

1     var str="hello world!"
2     console.log(str.split(" "));    // ["hello", "world!"]

Str.replace (String to be replaced, String to be replaced) replaces some characters in the string with others.Best with regular use

 
1     var str="hello world!"
2     console.log(str.replace("l","*"));    // he*lo world!

str.charCodeAt() returns the unicode character indexed by the specified index

1     var str="hello world!"
2     console.log(str.charCodeAt(0));    // 104

str.concat() Stitches two strings and returns a new string without changing the original string.

1     var str="hello world!"
2     console.log(str.concat("d"));    // hello world!d
3     console.log(str);   //hello world!

str.match() Retrieves the specified value within a string or finds a match for one or more regular expressions.Place the found characters in an array and return an array.

1     var str="hello world!"
2     console.log(str.match("d"));    // ["d", index: 10, input: "hello world!", groups: undefined]

The str.search() method retrieves a substring specified in a string or a substring that matches a regular expression.

1     var str="hello world!"
2     console.log(str.search("d"));    // 10
3     console.log(str.search("l"));   //2

The above method is string, same as array, does not involve ES6!!

Notice if the method will affect the original array!!

 

 

Topics: Javascript ascii less