A review of common methods of js arrays

Organized the following array methods

  • join()
  • push() and pop()
  • shift() 和 unshift()
  • sort()
  • reverse()
  • concat()
  • slice()
  • splice()
  • indexOf() and lastIndexOf() (new in ES5)
  • forEach() (new in ES5)
  • map() (new in ES5)
  • filter() (new in ES5)
  • every() (new in ES5)
  • some() (new in ES5)

1:join()

join, is to convert the array into a string, and then specify a connection character for him, the default is comma (,)

  Writing format: join(" "), write a string in parentheses ("quotation marks"),

var arr = [1,2,3];
console.log(arr.join());     // 1,2,3
console.log(arr.join("-"));   // 1-2-3
console.log(arr);         // [1, 2, 3](原数组不变)

2: push() and pop()

push(): Add the contents inside to the end of the array and return the modified length.

pop(): Remove the last item of the array, return the removed value, and reduce the length of the array.

    Writing format: arr.push(" "), write content in parentheses ("string should be quoted"),

      Writing format: arr.pop()

var arr = ["Lily","lucy","Tom"];
var count = arr.push("Jack","Sean");
console.log(count);           // 5
console.log(arr);            // ["Lily", "lucy", "Tom", "Jack", "Sean"]
var item = arr.pop();
console.log(item);            // Sean
console.log(arr);             // ["Lily", "lucy", "Tom", "Jack"]

3: unshift() and shift() are opposite to push() and pop()

unshift: Add the parameter to the beginning of the original array and return the length of the array.

shift(): Delete the first item of the original array and return the value of the deleted element; if the array is empty, return undefined. 

Writing format: arr.unshift(" "), write content in parentheses ("string should be quoted"),

Writing format: arr.shift()

var arr = ["Lily","lucy","Tom"];
var count = arr.unshift("Jack","Sean");
console.log(count);               // 5
console.log(arr);                //["Jack", "Sean", "Lily", "lucy", "Tom"]
var item = arr.shift();
console.log(item);               // Jack
console.log(arr);                // ["Sean", "Lily", "lucy", "Tom"]

4:sort()

sort(): Sort the items in the array from small to large

      Writing format: arr.sort()

var arr1 = ["a", "d", "c", "b"];
console.log(arr1.sort());           // ["a", "b", "c", "d"]

The sort() method compares strings, and does not sort the numbers according to the size of the value. To achieve this, you must use a sorting function

function sortNumber(a,b)
{
  return a - b
}
arr = [13, 24, 51, 3]; console.log(arr.sort());           // [13, 24, 3, 51] 
console.log(arr.sort(sortNumber));     // [3, 13, 24, 51](数组被改变)

5:reverse()

reverse(): Reverse the order of the array items.

      Writing format: arr.reverse()

var arr = [13, 24, 51, 3];
console.log(arr.reverse());         //[3, 51, 24, 13]
console.log(arr);               //[3, 51, 24, 13](原数组改变)

6:concat()

concat(): Add parameters to the original array. This method will first create a copy of the current array, then add the received parameters to the end of the copy, and finally return the newly constructed array. Without passing parameters to the concat() method, it just copies the current array and returns a copy.

    Writing format: arr.concat(), write content in parentheses ("string should be quoted"),

var arr = [1,3,5,7];
var arrCopy = arr.concat(9,[11,13]);
console.log(arrCopy);             //[1, 3, 5, 7, 9, 11, 13]
console.log(arr);               // [1, 3, 5, 7](原数组未被修改)

7:slice()

slice(): Returns a new array consisting of items from the specified start subscript to the end subscript in the original array. The slice() method can accept one or two parameters, that is, the starting and ending positions of the item to be returned. In the case of only one parameter, the slice() method returns all items from the position specified by the parameter to the end of the current array. If there are two parameters, the method returns the items between the start and end positions-but does not include the items at the end position.

    Writing format: arr.slice( 1, 3)

var arr = [1,3,5,7,9,11];
var arrCopy = arr.slice(1);
var arrCopy2 = arr.slice(1,4);
var arrCopy3 = arr.slice(1,-2);
var arrCopy4 = arr.slice(-4,-1);
console.log(arr);               //[1, 3, 5, 7, 9, 11](原数组没变)
console.log(arrCopy);             //[3, 5, 7, 9, 11]
console.log(arrCopy2);            //[3, 5, 7]
console.log(arrCopy3);            //[3, 5, 7]
console.log(arrCopy4);            //[5, 7, 9]

arrCopy only sets one parameter, that is, the starting subscript is 1, so the returned array is subscript 1 (including subscript 1) from the beginning to the end of the array. 

  arrCopy2 sets two parameters and returns the sub-array from the start index (including 1) to the end index (excluding 4). 

  arrCopy3 sets two parameters, the termination subscript is a negative number, when a negative number appears, the negative number is added to the value of the array length (6) to replace the number at that position, so it is a sub-array starting from 1 to 4 (not included) . 

  Both parameters in arrCopy4 are negative numbers, so both add array length 6 to convert them into positive numbers, so it is equivalent to slice(2,5).

8:splice()

splice(): delete, insert, and replace.

Delete: Specify 2 parameters: the position of the first item to be deleted and the number of items to be deleted.

    Writing format: arr.splice( 1, 3)

Insert: You can insert any number of items into the specified position, you only need to provide 3 parameters: the starting position, 0 (the number of items to be deleted) and the item to be inserted.

    Writing format: arr.splice( 2,0,4,6)
Replacement: you can insert any number of items into the specified position, and delete any number of items at the same time, you only need to specify 3 parameters: the starting position, the item to be deleted Number and any number of items to be inserted. The number of items inserted does not have to be equal to the number of items deleted.

    Writing format: arr.splice( 2,0,4,6)

var arr = [1,3,5,7,9,11];
var arrRemoved = arr.splice(0,2);
console.log(arr);                //[5, 7, 9, 11]
console.log(arrRemoved);            //[1, 3]
var arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr);                // [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2);           // []
var arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr);                // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3);           //[7]

9:indexOf()和lastIndexOf()

indexOf(): Receives two parameters: the item to be searched and (optional) the index indicating the starting point of the search. Among them, search backwards from the beginning of the array (position 0).

    Writing format: arr.indexof( 5)

lastIndexOf(): Receives two parameters: the item to be searched and (optional) the index indicating the starting point of the search. Among them, search forward from the end of the array. 

    Writing format: arr.lastIndexOf(5,4)

var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5));       //2
console.log(arr.lastIndexOf(5));     //5
console.log(arr.indexOf(5,2));      //2
console.log(arr.lastIndexOf(5,4));   //2
console.log(arr.indexOf("5"));      //-1

10:forEach()

forEach(): traverse the array and run the given function for each item in the array. This method has no return value. The parameters are all function types, which are passed by default. The parameters are: the content of the array to be traversed; the index of the corresponding array, the array itself.

    Writing format: arr.forEach()

var arr = [1, 2, 3, 4, 5];
arr.forEach(function(x, index, a){
console.log(x + '|' + index + '|' + (a === arr));
});
// 输出为:
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true

11:map()

map(): Refers to "mapping", runs a given function on each item in the array, and returns an array composed of the results of each function call.

    Writing format: arr.map()

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.map(function(item){
return item*item;
});
console.log(arr2);         //[1, 4, 9, 16, 25]

12:filter()

filter(): "Filter" function, each item in the array runs a given function and returns an array that meets the filter conditions.

    Writing format: arr.filter()

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr2 = arr.filter(function(x, index) {
return index % 3 === 0 || x >= 8;
}); 
console.log(arr2);         //[1, 4, 7, 8, 9, 10]

13:every()

every(): Determine whether each item in the array satisfies the condition, and only if all items satisfy the condition will it return true.

    Writing format: arr.every()

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.every(function(x) {
return x < 10;
}); 
console.log(arr2);         //true
var arr3 = arr.every(function(x) {
return x < 3;
}); 
console.log(arr3);         // false

14:some()

some(): Determine whether there is an item that meets the condition in the array, as long as there is one item that meets the condition, it will return true.

    Writing format: arr.some()

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.some(function(x) {
return x < 3;
}); 
console.log(arr2);         //true
var arr3 = arr.some(function(x) {
return x < 1;
}); 
console.log(arr3);         // false

15: A certain attribute value in the js object array is spliced ​​into a string

var objs=[
    {id:1,name:'张三'},
    {id:2,name:'李四'},
    {id:3,name:'王五'},
    {id:4,name:'赵六'},
];

var idsStr = objs.map(function(obj,index){
    return obj.id;
}).join(",");

console.log(idsStr);

16 js intercepts the content of a string in front of a string

   js intercepts the content between two strings:

var str = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)fff/)[1];
alert(str);//结果bbbcccdddeee

  js intercepts the content in front of a string:

var str = "aaabbbcccdddeeefff";
str = str.match(/(\S*)fff/)[1];
alert(str);//结果aaabbbcccddd

  js intercepts the content behind a string:

var str = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)/)[1];
alert(str);//结果bbbcccdddeeefff

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/AN0692/article/details/109511639